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

首頁 > 語言 > JavaScript > 正文

詳解angular 中的自定義指令之詳解API

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

自定義屬性的四種類別

分為: 元素E,屬性A,注釋M,類C , 分別如下:

 <my-dir></my-dir> <span my-dir="exp"></span> <!-- directive: my-dir exp --> <span class="my-dir: exp;"></span>

簡單創建一個指令

html結構:

<div ng-controller="myCtrl"> <div my-customer></div></div>

JavaScript結構:

angular.module('myApp', [])  .controller('myCtrl', ['$scope', function($scope) {   $scope.customer = {    name: 'Naomi',    address: '1600 Amphitheatre'   };  }])  .directive('myCustomer', function() {   return {    template: 'Name: {{customer.name}} Address: {{customer.address}}'   };  });

輸出:

Name: Naomi Address: 1600 Amphitheatre

說明: 此處,myCtrl 中定義的 $scope.customer 屬性和屬性值都在指令中的模板使用了。同樣的,在指令return 對象中的 template 也可被替換成一路徑,在路徑html中書寫和template中同樣的代碼,使用這種方式,可以操作更多代碼。

templateUrl 函數式編程

html結構:

<div ng-controller="myCtrl">  <div my-customer></div></div>

javascript結構:

 angular.module('myApp', []) .controller('myCtrl', ['$scope', function($scope) {  $scope.customer = {   name: 'Naomi',   address: '1600 Amphitheatre'  };   }]) .directive('myCustomer', function() {  return {   templateUrl: function(elem, attr) {    return 'customer-' + attr.type + '.html';   }  }; });

不同的templateUrl ①

 Name: {{customer.name}}

不同的templateUrl ②

 Address: {{customer.address}}

輸出結果:

Name: Naomi
Address: 1600 Amphitheatre

說明: templateUrl 的值可以是一個函數返回值,返回用于指令中的html模板的url。

隔離指令的作用域

① 通過不同的controller

html結構:

<div ng-app="myApp">  <div ng-controller="myCtrl1">    <my-customer></my-customer>  </div>  <div ng-controller="myCtrl2">    <my-customer></my-customer>  </div></div>

javascript結構:

angular.module('myApp', [])  .controller('myCtrl1', ['$scope', function($scope) {   $scope.customer = {    name: 'Naomi',    address: '1600 Amphitheatre'   };  }])  .controller('myCtrl2', ['$scope', function($scope) {   $scope.customer = {    name: 'Igor',    address: '123 Somewhere'   };  }])  .directive('myCustomer', function() {   return {    restrict: 'E',    templateUrl: 'my-customer.html'   };  });

templateUrl html 結構:

 Name: {{customer.name}} Address: {{customer.address}}            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 平泉县| 乌鲁木齐市| 红原县| 安平县| 苍溪县| 洞口县| 邯郸市| 兰考县| 贵港市| 兴隆县| 云南省| 柏乡县| 双辽市| 望都县| 葫芦岛市| 铁岭市| 文山县| 广丰县| 遵义县| 林口县| 防城港市| 明溪县| 壤塘县| 云南省| 瓮安县| 嘉黎县| 贺州市| 星子县| 上杭县| 于都县| 西丰县| 宁强县| 鄱阳县| 教育| 慈溪市| 吴旗县| 禄丰县| 霸州市| 和平区| 道真| 兴海县|