Dynamic Attributes with AngularJS

前端 未结 2 1411
终归单人心
终归单人心 2021-01-17 12:11

In some cases I need to apply different attributes to a node based on properties in my model.

For example, in one case I need to add a \'required\' tag and in anothe

相关标签:
2条回答
  • 2021-01-17 12:56

    I have quickly created a directive that allows you specify attributes dynamically.

    http://jsfiddle.net/HB7LU/1806/

    I'm not sure if it will have the desired effect you are after in this simple form, but it might be a good starting point. You essentially use:

    <div dyn-attrs="someModelArray"></div>

    And set your model accordingly:

    $scope.someModelArray = [
        { attr: 'myattribute', value: '' },
        { attr: 'anotherattribute', value: 'val' }
    ];
    
    0 讨论(0)
  • 2021-01-17 12:58

    In this case it would be best to make use of ngRequired:

    <input class="form-control" type="text" ng-required="model.required" />
    
    0 讨论(0)
提交回复
热议问题