angularjs 我自定义了一个指令,想在同一个页面复用

自定义了一个指令,想在一个页面中多次使用,每个指令传入的参数不同,如何让他获取对应参数取得的值
html
<select-option title="性别" grup="sex" ></select-option>
<select-option title="血型" grup="blood" ></select-option>
js
app.directive('selectOption',function($http){

return{
    restrict:'E',
    scope :'' ,
    link:function(scope, element, attrs) {
        scope.title = attrs.title;
        scope.grup = attrs.grup;
        scope.list = attrs.grup;
        var data = {
                 'groupName':attrs.title
        };
        scope.ceshi = function(a){
            alert(a);
        }
        $http.post('/datadicItem/findItemsByGroupName',data,postCfg)
             .success(function(resp){
                 // debugger
                 scope.list = resp;
                 console.log(scope.list);
             });
    },
    template:'<div style="text-align:center;height:30px;">'+
    '<span class="r" style="margin-top:4px;width:120px;"> '+
    '<span  style="vertical-align: 12px;">&nbsp;&nbsp;{{title}}'+
        '<select style="margin-left:5px;"'+
            'ng-model="grup"'+
            'ng-change="ceshi(grup)"'+
            'ng-options="act.id as act.itemname for act in list">'+'<option value="">'+'--请选择--'+'</option>'+
        '</select>'+
    '</span>'+
    '</span>'+
    '</div>',
    replace:true,
};

});

阅读 4.4k
2 个回答

option选项里的具体内容 如果是经常变的 要向后端发起请求获取 如果是性别只有男女这种 就可以写在controller里面

下面具体代码没看完,就独立指令,上面应该这么写:

return {
    restrict : 'E',
    scope : {
        title : '=',
        grup  : '='
    },
    link : function( scope, tElement, tAttrs ){
        // 这样数据才是双向绑定的~
        // scope.title
        // scope.grup
        ...
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题