AngularJS实现自定义指令及指令配置项的方法

前端之家收集整理的这篇文章主要介绍了AngularJS实现自定义指令及指令配置项的方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

@H_404_0@本文实例讲述了AngularJS实现自定义指令及指令配置项的方法分享给大家供大家参考,具体如下:


@H_404_0@<span style="font-size: medium">

AngularJS自定义指令有两种写法:


<div class="jb51code">
<pre class="brush:js;">
//第一种
angular.module('MyApp',[])
.directive('zl1',zl1)
.controller('con1',['$scope',func1]);
function zl1(){
var directive={
restrict:'AEC',template:'this is the it-first directive',};
return directive;
};
function func1($scope){
$scope.name="alice";
}
//第二种
angular.module('myApp',[]).directive('zl1',[ function(){
return {
restrict:'AE',template:'thirective',link:function($scope,elm,attr,controller){
console.log("这是link");
},controller:function($scope,$element,$attrs){
console.log("这是con");
}
};
}]).controller('Con1',function($scope){
$scope.name="aliceqqq";
}]);

@H_404_0@指令配置项

修改这里的name时,second会在自己的作用域中新建一个name变量,与父级作用域中的 // name相对独立,所以再修改父级中的name对second中的name就不会有影响了 template: 'second name:{{name}}',}; }]).directive('third',[ function(){ return { scope: {},// 创建指令自己的独立作用域,与父级毫无关系 // controller: function($scope,M = Comment template: 'third name:{{name}}',}; }]) .controller('DirectiveController',function($scope){ $scope.name="mike"; }]);
@H_404_0@更多关于AngularJS相关内容感兴趣的读者可查看本站专题:《》、《》及《

@H_
404_0@希望本文所述对大家AngularJS程序设计有所帮助。

猜你在找的JavaScript相关文章