首頁  >  文章  >  後端開發  >  javascript - angular自定义指令如何写

javascript - angular自定义指令如何写

WBOY
WBOY原創
2016-07-06 13:51:35781瀏覽

现在我有这样三段类似的代码

<code>       <input type="radio" name="a" ng-value="planStatuse.id" ng-model="planStatus.status" ng-click="planStatusChange()">
       <span>{{planStatuse.name}}</span></code>

<code>                <input type="radio" ng-value="{{_appType.id}}" ng-model="appType.val" name="app">
                <span>{{_appType.name}}</span></code>

<code>                <input type="radio" name="center" ng-model="pageType" ng-value="centerPage.id" ng-click="changePage(pageType)">
                <span>{{centerPage.name}}</span></code>


我要把这三块代码写成一个自定义指令 ,但是以上代码有的有事件,有的没有事件,而且事件不一样,该如何写?

var eInput= angular.module("eInput", []);
eInput.directive('eInput',function(){

<code>return {
    restrict: "A",
    template:' <div ng-repeat="inputList in inputListes track by $index" class="planlist">'+
   ' <input type="radio" name="a" ng-value="inputList.id" ng-model="inputList.status" ng-click="planStatusChange($index)">'+
    '<span>{{inputList.name}}</span>'+
'</div>',
    replace:true,
    scope:{
        inputListes:'='
    }
}
</code>

});

回复内容:

现在我有这样三段类似的代码

<code>       <input type="radio" name="a" ng-value="planStatuse.id" ng-model="planStatus.status" ng-click="planStatusChange()">
       <span>{{planStatuse.name}}</span></code>

<code>                <input type="radio" ng-value="{{_appType.id}}" ng-model="appType.val" name="app">
                <span>{{_appType.name}}</span></code>

<code>                <input type="radio" name="center" ng-model="pageType" ng-value="centerPage.id" ng-click="changePage(pageType)">
                <span>{{centerPage.name}}</span></code>


我要把这三块代码写成一个自定义指令 ,但是以上代码有的有事件,有的没有事件,而且事件不一样,该如何写?

var eInput= angular.module("eInput", []);
eInput.directive('eInput',function(){

<code>return {
    restrict: "A",
    template:' <div ng-repeat="inputList in inputListes track by $index" class="planlist">'+
   ' <input type="radio" name="a" ng-value="inputList.id" ng-model="inputList.status" ng-click="planStatusChange($index)">'+
    '<span>{{inputList.name}}</span>'+
'</div>',
    replace:true,
    scope:{
        inputListes:'='
    }
}
</code>

});

你确认一下你每个要执行的函数是否都在controller作用域里

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn