国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

詳解AngularJS1.x學習directive 中‘& ’‘=’ ‘@’符號的區別使用

2024-05-06 15:12:24
字體:
來源:轉載
供稿:網友

對于一個Html5框架的好壞,我們有幾個評判標準, 輕量級,可拓展,易復用,速度快。

對組件復用這點,angular以directive的形式展示給開發者,是一個還算不錯的選擇,作為一個UI組件,必定存在數據交互。

那么數據交互過程中的幾個符號我們一定要有所了解,以及他們的區別是什么,防止我們在運用過程中出錯。

1. 首先,我們看一scope作用域下面@的使用:

html

<!doctype html> <html ng-app='myApp'>   <head>    </head>   <body>     <div ng-controller="listCtrl">     <input type="text" ng-model="t" />    <test title="{{t}}" >     <span>我的angularjs</span>   </test> </div>  <script type="text/javascript" src="angular.js"></script> <script type="text/javascript" src="main.js"></script> </body></html> 

js

var myApp=angular.module('myApp',[]); myApp.controller('listCtrl',function($scope){   $scope.logchore="motorola"; }); myApp.directive('test',function(){   return {     'restrict':'E',     scope:{       title:"@"     },     template:'<div >{{title}}</div>'   } }); 

這個必須指定的,這里的title是指令里scope的@對應的,t就是控制域scope下的 .

2. = 的使用。

html

<!doctype html> <html ng-app='myApp'>   <head>    </head>   <body>     <div ng-controller="listCtrl">     <input type="text" ng-model="t" />    <test title="t" >     <p>{{title}}</p>     <span>我的angularjs</span>   </test> </div>  <script type="text/javascript" src="angular.js"></script> <script type="text/javascript" src="main05.js"></script> </body></html> 

js

var myApp=angular.module('myApp',[]); myApp.controller('listCtrl',function($scope){   $scope.logchore="motorola"; }); myApp.directive('test',function(){   return {     'restrict':'E',     scope:{       title:"="     },     template:'<div >{{title}}</div>'   } }); 

和上面@相比,這個直接賦值等于scope域下的t了

3. 最好我們看看&符號的使用

html

<!doctype html> <html ng-app='myApp'>   <head>    </head>   <body>     <div ng-controller="listCtrl">      <test flavor="logchore()" ></test> </div>  <script type="text/javascript" src="angular.js"></script> <script type="text/javascript" src="main05.js"></script> </body></html> 

js

var myApp=angular.module('myApp',[]); myApp.controller('listCtrl',function($scope){   $scope.logchore=function(){     alert('ok');   }; }); myApp.directive('test',function(){   return {     'restrict':'E',     scope:{       flavor:"&"      },     template:'<div ><button ng-click="flavor()"></button></div>'   } });             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 宁阳县| 侯马市| 尉犁县| 嵊泗县| 灯塔市| 尚志市| 延川县| 朝阳市| 赤峰市| 石首市| 南华县| 辛集市| 安仁县| 商南县| 泸水县| 金湖县| 黄骅市| 内江市| 灌南县| 鞍山市| 商洛市| 墨竹工卡县| 宁陵县| 志丹县| 临夏县| 修武县| 广安市| 普兰县| 绿春县| 民乐县| 天水市| 锦屏县| 象州县| 肥西县| 师宗县| 乌鲁木齐市| 永定县| 永定县| 诏安县| 嵊州市| 诏安县|