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

首頁 > 編程 > JavaScript > 正文

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

2019-11-19 15:42:05
字體:
來源:轉載
供稿:網友

對于一個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>'   } }); 

嘗試一下,就明白了,簡潔明了!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新兴县| 尚志市| 万安县| 鹤岗市| 平江县| 福清市| 孟津县| 韶关市| 平度市| 堆龙德庆县| 永清县| 丽水市| 呼和浩特市| 南丰县| 台山市| 佛山市| 新晃| 安阳县| 玉田县| 西安市| 阿尔山市| 姚安县| 禹城市| 肥东县| 年辖:市辖区| 普宁市| 旬阳县| 曲麻莱县| 沛县| 招远市| 浙江省| 庄河市| 广德县| 青岛市| 余干县| 镇宁| 宾阳县| 乌兰浩特市| 万盛区| 宁武县| 富宁县|