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

Angular.JS中的指令引用template與指令當做屬性詳解

 更新時間:2017年03月30日 11:30:03   作者:多客博圖  
這篇文章主要介紹了Angular.JS中的指令引用template與指令當做屬性的相關資料,文中介紹的非常詳細,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。

一、引用template

對于指令,可以把它簡單的理解成在特定DOM元素上運行的函數(shù),指令可以擴展這個元素的功能。

指令要生效,那么html頭里面要

<html lang="en" ng-app="app">

制定ng-app的值和定義指令的module名字一致:

angular.module('app',[])

指令的完整參數(shù):

angular.module('myApp', [])
.directive('myDirective', function() {
 return {
 restrict: String,
 priority: Number,
 terminal: Boolean,
 template: String or Template Function:
 function(tElement, tAttrs) {...},
 templateUrl: String,
 replace: Boolean or String,
 scope: Boolean or Object,
 transclude: Boolean,
 controller: String or
 function(scope, element, attrs, transclude, otherInjectables) { ... },
 controllerAs: String,
 require: String,
 link: function(scope, iElement, iAttrs) { ... },
 compile: // 返回一個對象或連接函數(shù),如下所示: function(tElement, tAttrs, transclude) {
 return {
 pre: function(scope, iElement, iAttrs, controller) { ... },
 post: function(scope, iElement, iAttrs, controller) { ... }
 }
 return function postLink(...) { ... }
 }
 };
 });

指令可以使用的方式:

restrict[string]

restrict是一個可選的參數(shù)。用于指定該指令在DOM中以何種形式被聲明。默認值是A,即以屬性的形式來進行聲明。

可選值如下:

  • E(元素)<my-directive></my-directive>
  • A(屬性,默認值)<div my-directive="expression"></div>
  • C(類名)<div class="my-directive:expression;"></div>
  • M(注釋)<--directive:my-directive expression-->

replace[bool]

replace是一個可選參數(shù),如果設置了這個參數(shù),值必須為true,因為默認值為false。默認值意味著模板會被當作子元素插入到調用此指令的元素內部,

例如上面的示例默認值情況下,生成的html代碼如下:

<my-directive value="http://www.baidu.com" text="百度"><a  rel="external nofollow" rel="external nofollow" rel="external nofollow" >百度</a></my-directive>

如果設置replace=true

<a  rel="external nofollow" rel="external nofollow" rel="external nofollow" value="http://www.baidu.com" text="百度">百度</a>

據(jù)我觀察,這種效果只有設置restrict="E"的情況下,才會表現(xiàn)出實際效果。

template[string or function]

template參數(shù)是可選的,必須被設置為以下兩種形式之一:

 一段HTML文本;

一個可以接受兩個參數(shù)的函數(shù),參數(shù)為tElement和tAttrs,并返回一個代表模板的字符串。tElement和tAttrs中的t代表template,是相對于instance的。

不管是返回html文本還是函數(shù),都是最后替換一個html,和replace屬性搭配使用的,先給出一個完整的index.heml directive.js,以這個為例子來說明:

<!doctype html>
<html lang="en" ng-app="app">

<head>
 <meta charset="utf-8">
 <title>My HTML File</title>
 <link rel="stylesheet" href="bootstrap/css/bootstrap.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
 <script src="angularjs/angular.js"></script>
 <script src="mydirective.js"></script>
</head>

<body>
 <my-directive></my-directive>
</body>

</html>

然后js:

 angular.module('app',[])
 .directive('myDirective', function () {
 return {
 restrict: 'E',
 template: '<a  rel="external nofollow" rel="external nofollow" rel="external nofollow" >百度</a>'
 };
 })

最后的運行效果以及firebug查看到的效果:

如果添加指令的replace屬性為ture,那么就不會有這個directvie指令部分了:

上面就是差別。

再說說指令定義里面模板參數(shù)是函數(shù)的情況,我們改寫html以及js:

<!doctype html>
<html lang="en" ng-app="app">

<head>
 <meta charset="utf-8">
 <title>My HTML File</title>
 <link rel="stylesheet" href="bootstrap/css/bootstrap.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
 <script src="angularjs/angular.js"></script>
 <script src="mydirective.js"></script>
</head>

<body>
 <my-directive value="http://www.baidu.com" text="百度"></my-directive>
</body>

</html>

js文件:
angular.module('app',[])
 .directive('myDirective', function () {
 return {
 restrict: 'EAC',
 template: function (elem, attr) {
  return "<a href='" + attr.value + "'>" + attr.text + "</a>";
 }
 };
 })

指令定義的template是一個函數(shù)對象,返回一個html的字符串,那么他的elem和attr就是分別代表這個指令和他在index.html里面的屬性:

attr.value和attr.text分別對應:

<my-directive value="http://www.baidu.com" text="百度"></my-directive>

里面的value和text。

不replace的情況:

二、指令當做屬性

上面說過:

angular.module('myApp', []) 
 .directive('myDirective', function() { 
 return { 
 restrict: String, 后面省略

指令restrict有四種用法,默認是A,也就是當做屬性,

  • E(元素)<my-directive></my-directive>
  • A(屬性,默認值)<div my-directive="expression"></div>
  • C(類名)<div class="my-directive:expression;"></div>
  • M(注釋)<--directive:my-directive expression-->

然后如果一個指令直接返回一個函數(shù)的時候,其實返回的一個link函數(shù),比如:

angular.module('time', [])
 .directive('xxx', function() {
 return function(scope, element, attrs) {

這個是表示直接link。

當指令做屬性的時候,有兩重含義:

      1.在一個html元素里面制定為屬性

      2.js定義的指令名。

看一個例子:

JS:

angular.module('time', [])
 .controller("Ctrl2", function($scope) {
 $scope.format = 'M/d/yy h:mm:ss a';
 })
 // Register the 'myCurrentTime' directive factory method.
 // We inject $timeout and dateFilter service since the factory method is DI.
 .directive('myCurrentTime', function($timeout, dateFilter) {
 // return the directive link function. (compile function not needed)
 return function(scope, element, attrs) {
  var format, // date format
  timeoutId; // timeoutId, so that we can cancel the time updates

  // used to update the UI
  function updateTime() {
  element.text(dateFilter(new Date(), format));
  }

  // watch the expression, and update the UI on change.
  scope.$watch(attrs.myCurrentTime, function(value) {
  format = value;
  updateTime();
  });

  // schedule update in one second
  function updateLater() {
  // save the timeoutId for canceling
  timeoutId = $timeout(function() {
   updateTime(); // update DOM
   updateLater(); // schedule another update
  }, 1000);
  }

  // listen on DOM destroy (removal) event, and cancel the next UI update
  // to prevent updating time ofter the DOM element was removed.
  element.bind('$destroy', function() {
  $timeout.cancel(timeoutId);
  });

  updateLater(); // kick off the UI update process.
 }
 });

然后index.html:

<!doctype html>
<html lang="en" ng-app="time">

<head>
 <meta charset="utf-8">
 <title>My HTML File</title>
 <link rel="stylesheet" href="bootstrap/css/bootstrap.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
 <script src="angularjs/angular.js"></script>
 <script src="mydirective.js"></script>
</head>

<body>
 <div ng-controller="Ctrl2">
 Date format:
 <input ng-model="format">
 <hr/>
 Current time is: <span my-current-time="format"></span>
 </div>
</body>

</html>

注意:ng-app="time"一定要指明是time。否則無法關聯(lián)起來。

分析如下:

  • 給span制定了一個屬性,綁定到了scope里面的format
  • <span my-current-time="format"></span>
  • 定義了輸入框,綁定了scope里面的format
  • <input ng-model="format">
  • 定義了controller -- Ctrl2, 然后引入了scope,定義了變量format
  • 定義了指令myCurrentTime , 然后就是和html里面的my-current-time="format"對應,html里面用破折號連起來,指令就是駝峰樣子的myCurrentTime(首字母小寫)
  • link函數(shù)的三個參數(shù),以及attrs的使用:
    return function(scope, element, attrs) {
    scope.$watch(attrs.myCurrentTime, function(value) {
  • 可看到,myCurrentTime既是指令名字,然后在這個span元素里面又是屬性名,他的值是format的真實值。
  • 用firebug看到:
  • 指令當成屬性,不會有replace起作用的時候,不會被替換也不會插入,就是一個屬性,后面的日期值,其實是updateTime()函數(shù)直接寫elem.text的效果。
  • 此處指令當做屬性的作用就是擴展當前元素的功能。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關文章

最新評論

遵义市| 万盛区| 海兴县| 张掖市| 怀远县| 西乡县| 平顶山市| 玛曲县| 科技| 林芝县| 莆田市| 阿鲁科尔沁旗| 松阳县| 达孜县| 冷水江市| 连平县| 洛南县| 安阳市| 定日县| 泽州县| 敦煌市| 屏东市| 龙里县| 长宁县| 酒泉市| 怀远县| 海淀区| 昌图县| 承德县| 东安县| 蓬溪县| 海城市| 贡山| 沈丘县| 砚山县| 乳山市| 扶余县| 舟山市| 二连浩特市| 遂宁市| 泸西县|