4 回答
TA贡献1777条经验 获得超3个赞
路由知道配了后,.controller('xxxxCtrl', ['$scope',function($scope){
$scope.test=function(){
console.log("这就是方法1");
}
}
在xxxx页面中
<div ng-click="test()">1111</div>
点击1111就触发了函数方法test()
TA贡献1906条经验 获得超3个赞
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <!doctype html> <html> <head> <meta charset="utf-8"> <title>test</title> <script src="angular.min.js"></script> <script> var myApp = angular.module("myApp", []); myApp.directive('testIt',function(){ return { restrict: 'A', scope: false, link:function(scope, elm, attr){ if(scope.$last){ elm.css('color','red'); scope.myFunction(); } } } }); myApp.controller("testCtrl", function($scope){ $scope.items = [0,1,2,3,4]; $scope.myFunction = function(){ console.log('Hello!'); }; }); </script> </head>
<body> <div ng-app="myApp"> <div ng-controller="testCtrl"> <ul> <li ng-repeat="item in items" test-it >{{item}}</li> </ul> </div> </div> </body> </html> |
主要在于12行的scope:false,这个是默认的,其实你不写也是false。这样drective继承了父scope,所以可以调用父作用域的方法,而声明新的scope即scope:{}形式就不会继承了,不过你依然可以用scope.$parent.myFunction()的方式调用。
TA贡献1784条经验 获得超9个赞
A',
scope: false,
link:function(scope, elm, attr){
if(scope.$last){
elm.css('color','red');
scope.myFunction();
}
}
}
});
myApp.controller("testCtrl", function($scope){
$scope.items = [0,1,2,3,4];
$scope.myFunction = function(){
console.log('Hello!');
};
});
</script>
- 4 回答
- 0 关注
- 1261 浏览
添加回答
举报