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

首頁 > 編程 > JavaScript > 正文

angularjs自定義ng-model標簽的屬性

2019-11-20 10:45:18
字體:
來源:轉載
供稿:網友

有的時候我們需要為非input類型的元素添加ng-model來實現雙向的數據綁定,從而減少冗余代碼,那么可以嘗試一下的方式

例如:我頁面中使用了contenteditable這個屬性來實現用戶可直接編譯的div元素

html:

 <style>    .text{      margin:0 auto;      width:100px;      height:50px;      border:1px solid red;    }  </style></head><body><div ng-controller="selectController">  <div ng-repeat="pop in citylist">    <div class="text" contenteditable="true" ng-model="pop.pop"></div>  </div>  <button ng-click="cs()">輸出新數據</button></div></body>

但是直接綁定ng-model是肯定得不到數據的,這時就需要為其增加自定義的屬性,如下所示。

js:

<script>  var app = angular.module('app', []);  app.controller('selectController', function ($scope) {    $scope.citylist=[{id:1,pop:"北京"},{id:1,pop:"上海"},{id:1,pop:"廣州"}];    $scope.p={};    $scope.cs=function(){      console.log($scope.citylist);    }  }).directive('contenteditable', function() {//自定義ngModel的屬性可以用在div等其他元素中    return {      restrict: 'A', // 作為屬性使用      require: '?ngModel', // 此指令所代替的函數      link: function(scope, element, attrs, ngModel) {        if (!ngModel) {          return;        } // do nothing if no ng-model        // Specify how UI should be updated        ngModel.$render = function() {          element.html(ngModel.$viewValue || '');        };        // Listen for change events to enable binding        element.on('blur keyup change', function() {          scope.$apply(readViewText);        });        // No need to initialize, AngularJS will initialize the text based on ng-model attribute        // Write data to the model        function readViewText() {          var html = element.html();          // When we clear the content editable the browser leaves a <br> behind          // If strip-br attribute is provided then we strip this out          if (attrs.stripBr && html === '<br>') {            html = '';          }          ngModel.$setViewValue(html);        }      }    };  })</script>

其中參數類別如下:

部分參數解釋

restrict:

(字符串)可選參數,指明指令在DOM里面以什么形式被聲明;

取值有:E(元素),A(屬性),C(類),M(注釋),其中默認值為A;

E(元素):<directiveName></directiveName>
A(屬性):<div directiveName='expression'></div>
C(類):   <div class='directiveName'></div>
M(注釋):<--directive:directiveName expression-->

2.require

字符串代表另一個指令的名字,它將會作為link函數的第四個參數

具體用法我們可以舉個例子說明

假設現在我們要編寫兩個指令,兩個指令中的link鏈接函數中(link函數后面會講)存在有很多重合的方法,

這時候我們就可以將這些重復的方法寫在第三個指令的controller中(上面也講到controller經常用來提供指令間的復用行為)

然后在這兩個指令中,require這個擁有controller字段的的指令(第三個指令),

最后通過link鏈接函數的第四個參數就可以引用這些重合的方法了。

<!doctype html><html ng-app="myApp"><head> <script src="http://cdn.staticfile.org/angular.js/1.2.10/angular.min.js"></script></head><body> <outer-directive>   <inner-directive></inner-directive>   <inner-directive2></inner-directive2> </outer-directive> <script>  var app = angular.module('myApp', []);  app.directive('outerDirective', function() {     return {        scope: {},        restrict: 'AE',        controller: function($scope) {            this.say = function(someDirective) {            console.log('Got:' + someDirective.message);         };        }      };  });  app.directive('innerDirective', function() {     return {        scope: {},        restrict: 'AE',        require: '^outerDirective',        link: function(scope, elem, attrs, controllerInstance) {            scope.message = "Hi,leifeng";            controllerInstance.say(scope);        }     };  });  app.directive('innerDirective2', function() {     return {        scope: {},        restrict: 'AE',        require: '^outerDirective',        link: function(scope, elem, attrs, controllerInstance) {            scope.message = "Hi,shushu";            controllerInstance.say(scope);        }     };  }); </script></body></html>

上面例子中的指令innerDirective和指令innerDirective2復用了定義在指令outerDirective的controller中的方法

也進一步說明了,指令中的controller是用來讓不同指令間通信用的。

另外我們可以在require的參數值加上下面的某個前綴,這會改變查找控制器的行為:

(1)沒有前綴,指令會在自身提供的控制器中進行查找,如果找不到任何控制器,則會拋出一個error

(2)?如果在當前的指令沒有找到所需的控制器,則會將null傳給link連接函數的第四個參數

(3)^如果在當前的指令沒有找到所需的控制器,則會查找父元素的控制器

(4)?^組合

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 缙云县| 广元市| 九寨沟县| 遵义县| 楚雄市| 剑阁县| 巴林右旗| 四川省| 志丹县| 象州县| 安平县| 岳阳市| 临沧市| 连山| 高雄县| 桦甸市| 伊通| 喜德县| 东源县| 龙门县| 泰顺县| 乌鲁木齐市| 东兴市| 内丘县| 上蔡县| 毕节市| 巢湖市| 上林县| 江华| 格尔木市| 宁津县| 南阳市| 交口县| 康平县| 嘉荫县| 尉犁县| 嘉定区| 昌都县| 北票市| 循化| 泉州市|