简体   繁体   English

通过绑定在Angular 1.x中加载输入指令

[英]Load input directive by binding in Angular 1.x

I have an input like this: 我有这样的输入:

<input type="text" valid-text class="input-text" name="{{$ctrl.name}}">

the key thing is the valid-text directive. 关键是有效文本指令。 This input is a component and some times is valid-text and sometimes valid-number. 此输入是一个组件,有时是有效文本,有时是有效数字。 How can I set dynamically this value? 如何动态设置此值?

I tested valid-{{$ctrl.validType}} with no success. 我测试了有效的{{$ ctrl.validType}},但没有成功。

You can begin with the next directive that's get both model and attribute. 您可以从下一个同时获取模型和属性的指令开始。 Now it add both attribute string and number 现在,它同时添加属性string和数字

myApp.directive('dynAttr', function() {
return {
    scope: { list: '=dynAttr' },
    require: 'ngModel',
    link: function(scope, elem, attrs, ngModel){

        for(attr in scope.list){
            elem.attr(scope.list[attr].attr, scope.list[attr].value);  
            // you can change this logic
        }

    }
};
});

in yours controller 在您的控制器中

$scope.dynAttribute = [
    { attr: 'number', value: '' },
    { attr: 'string', value: '' }
];

and html 和html

<div ng-controller="MyCtrl">
    <input ng-model="val" dyn-attr="dynAttribute"/>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM