首页 > 代码库 > angularJS1笔记-(12)-自定义指令(compile/link)
angularJS1笔记-(12)-自定义指令(compile/link)
index.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> <div ng-app="myApp"> <div ng-controller="firstController"> <div ng-repeat="user in users" custom-tags><a href="http://www.mamicode.com/www.baidu.com">baidu</a></div> </div> </div> <script type="text/javascript" src="http://www.mamicode.com/vendor/angular/angularJs.js"></script> <script type="text/javascript" src="http://www.mamicode.com/app/index.js"></script> <script> </script> </body> </html>
index.js:
var myApp = angular.module(‘myApp‘, []) .directive(‘customTags‘,function () { return{ restrict:"ECAM", template:"<div>aaa<span ng-transclude></span></div>", replace:true, transclude:true, compile:function (tElement,tAttrs,transclude) { console.log(tElement); console.log(tAttrs); console.log(‘customTags compile‘); console.log("你好,我是编译阶段的方法"); //在编译阶段只执行一次 return function () { console.log(‘compile return function‘) } } } }) .controller(‘firstController‘,[‘$scope‘,function ($scope) { $scope.users = [ { id:10, name:‘张三‘ }, { id:20, name:‘李四‘ } ]; }]);
运行结果:
angularJS1笔记-(12)-自定义指令(compile/link)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。