0
我是AngularJs指令的新手。 我想要做的是,我需要添加一個簡單的類到模板指令中的一個元素點擊。 例如:如何將Class添加到模板指令元素中?
var app = angular.module("peykaar",[]);
app.controller('main', function($scope , $rootScope) {
$scope.arry = ["Apple","banana","orange"];
});
app.directive("customCheckbox", function() {
return {
restrict: 'A',
replace: true,
scope: {
name:"@"
},
link: function(scope, element, attrs){
var logic = {
init: function(elem){
var self = this;
elem.on('click', function(){
if (elem.hasClass('tik')){
self.unTik(elem);
} else if (elem.hasClass('untik')) {
self.tik(elem);
}
});
},
tik: function(elem){
elem.addClass('custom_checked tik');
elem.removeClass('untik');
},
unTik:function(elem){
elem.removeClass('custom_checked tik');
elem.addClass('untik');
}
};
logic.init(element);
},
template: '<div><span class="custom_checkbox untik"></span><p>{{name}}</p></div>'
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="peykaar" ng-controller="main">
<div class="arry">
\t \t <span ng-repeat="item in arry track by $index" custom-checkbox name="{{item}}" id="$index"></span>
\t </div>
</div>
那麼,如何使用類element.addClass( 「選中」)跨越類= 「custom_checkbox」
有一個指令:https://docs.angularjs.org/api/ng/directive/ngClass –
@ J.Titus謝謝你。 但我需要知道如何從鏈接功能訪問該元素。 有幫助嗎? –
對不起,這超出了我對Angular的(非常)有限的瞭解。祝你好運。 –