最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2016年01月21日 10:58:13   投稿:hebedich  
這篇文章主要介紹了angularjs自定義ng-model標簽的屬性的相關資料,需要的朋友可以參考下

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

例如:我頁面中使用了contenteditable這個屬性來實現(xiàn)用戶可直接編譯的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函數的第四個參數

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

假設現(xiàn)在我們要編寫兩個指令,兩個指令中的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)?^組合

相關文章

  • Angularjs中如何使用filterFilter函數過濾

    Angularjs中如何使用filterFilter函數過濾

    這篇文章主要介紹了Angularjs中如何使用filterFilter函數過濾的相關資料,需要的朋友可以參考下
    2016-02-02
  • AngularJS HTML編譯器介紹

    AngularJS HTML編譯器介紹

    這篇文章主要介紹了AngularJS HTML編譯器介紹,AngularJS的HTML編譯器能讓瀏覽器識別新的HTML語法。它能讓你將行為關聯(lián)到HTML元素或者屬性上,甚至能讓你創(chuàng)造具有自定義行為的新元素,需要的朋友可以參考下
    2014-12-12
  • angular.bind使用心得

    angular.bind使用心得

    這篇文章主要介紹了angular.bind使用心得,以及個人對于angular.bind的理解,這里分享給大家,希望大家能夠喜歡。
    2015-10-10
  • angular4強制刷新視圖的方法

    angular4強制刷新視圖的方法

    今天小編就為大家分享一篇angular4強制刷新視圖的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • 在Angular中實現(xiàn)懶加載的示例代碼

    在Angular中實現(xiàn)懶加載的示例代碼

    在Angular中,懶加載技術通過路由配置實現(xiàn)模塊的按需加載,可優(yōu)化應用啟動時間和減少初始加載代碼量,首先創(chuàng)建獨立模塊,在模板中使用<router-outlet>插入懶加載組件,并可采用預加載策略如PreloadAllModules,以提前加載所有懶加載模塊,優(yōu)化加載性能
    2024-10-10
  • AngularJS實用開發(fā)技巧(推薦)

    AngularJS實用開發(fā)技巧(推薦)

    Angular JS 是一組用來開發(fā)Web頁面的框架、模板以及數據綁定和豐富UI組件。接下來通過本文給大家介紹AngularJS實用開發(fā)技巧的相關資料,需要的朋友可以參考下
    2016-07-07
  • angular安裝import?echarts?from‘echarts‘標紅報錯解決

    angular安裝import?echarts?from‘echarts‘標紅報錯解決

    這篇文章主要介紹了angular安裝import?echarts?from‘echarts‘標紅報錯解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10
  • AngularJS使用ocLazyLoad實現(xiàn)js延遲加載

    AngularJS使用ocLazyLoad實現(xiàn)js延遲加載

    這篇文章主要介紹了AngularJS使用ocLazyLoad實現(xiàn)js延遲加載的相關資料,需要的朋友可以參考下
    2017-07-07
  • Angular2 組件通信的實例代碼

    Angular2 組件通信的實例代碼

    本篇文章主要介紹了Angular2 組件通信的實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • angular json對象push到數組中的方法

    angular json對象push到數組中的方法

    下面小編就為大家分享一篇angular json對象push到數組中的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02

最新評論

来凤县| 莱州市| 台东县| 黄骅市| 个旧市| 淮滨县| 平江县| 洛南县| 滕州市| 巢湖市| 扶沟县| 昆山市| 广元市| 鲁甸县| 澄城县| 屯留县| 太谷县| 库伦旗| 霍城县| 商丘市| 滕州市| 丹棱县| 楚雄市| 亳州市| 临桂县| 太康县| 象州县| 阳谷县| 竹北市| 新余市| 沧州市| 泸水县| 伊川县| 昌图县| 微山县| 屏山县| 吴川市| 乐陵市| 会同县| 宜兰市| 惠来县|