我的指令工作正常,但我想在ng-click
中使用它但是,鏈接內的函數不能被觸發。Angularjs指令鏈接調用函數ng-click
<div ng-app="editer" ng-controller="myCtrl" class="container">
<div class="parents">
<div ng-repeat="item in items" class="wrap" sibs>
<span>{{item.name}}</span>
<button ng-click="">click</button>
</div>
</div>
</div>
JS
function myCtrl($scope) {
$scope.editedItem = null;
$scope.items = [{
name: "item #1",
thing: "thing 1"
}, {
name: "item #2",
thing: "thing 2"
}, {
name: "item #3",
thing: "thing 3"
}];
$scope.show = false; //ignore this line
}
var editer = angular.module('editer', []);
editer.directive('sibs', function() {
return {
link: function(scope, element, attrs) {
element.bind('click', function() {
element.parent().children().addClass('unclicked');
element.removeClass('unclicked');
})
scope.myClick = function() {
element.parent().children().addClass('unclicked');
element.removeClass('unclicked');
}
},
}
});
我想調用的函數NG點擊請看這一個http://jsfiddle.net/ovzyro1f/2/從div ng-repeat="item in items" class="wrap"
<button ng-click="myClick()">click</button>
這對我的作品。我編輯了你的JSFilddle:http://jsfiddle.net/ovzyro1f/1/ –
是的,它正在工作,但我想在ng-click中調用該函數,請看這一個http://jsfiddle.net/ovzyro1f/ 2 /從div ng-repeat =「項目中的項目」class =「wrap」中刪除sib謝謝 – olo
爲什麼要刪除'sibs'? –