码迷,mamicode.com
首页 > 其他好文 > 详细

angular 自定义指令 link

时间:2015-09-22 18:34:53      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

function link(scope, element, attrs) { ... } where:

  • scope is an Angular scope object.
  • element is the jqLite-wrapped element that this directive matches.
  • attrs is a hash object with key-value pairs of normalized attribute names and their corresponding attribute values.


<div ng-controller="Controller"> Date format: <input ng-model="format"> <hr/> Current time is: <span my-current-time="format"></span> </div> angular.module(docsTimeDirective, []) .controller(Controller, [$scope, function($scope) { $scope.format = M/d/yy h:mm:ss a; }]) .directive(myCurrentTime, [$interval, dateFilter, function($interval, dateFilter) { function link(scope, element, attrs) { var format, timeoutId; function updateTime() { //format从watch方法里获得 element.text(dateFilter(new Date(), format)); } scope.$watch(attrs.myCurrentTime, function(value) { format = value;//value:M/d/yy h:mm:ss a updateTime(); }); element.on($destroy, function() { $interval.cancel(timeoutId); }); // start the UI update process; save the timeoutId for canceling timeoutId = $interval(function() { updateTime(); // update DOM }, 1000); } return { link: link }; }]);

 

angular 自定义指令 link

标签:

原文地址:http://www.cnblogs.com/xiaotaiyang/p/4829227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!