动态属性与AngularJS [英] Dynamic Attributes with AngularJS

查看:111
本文介绍了动态属性与AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,我需要不同的属性适用于根据我在模特属性的一个节点。

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

例如,在一个情况下,我需要添加一个必要的标签,并在另一种情况并非如此。我一直在使用NG-如果有不同的分支来实现这一点,但情况正在迅速失控。

For example, in one case I need to add a 'required' tag and in another case not. I've been using ng-if with different branches to accomplish this but the cases are getting out of hand quickly.

 <div ng-if="model.required">
    <input class="form-control"
           type="text"
           required 
           ng-model="model" />
 </div>
 <div ng-if="!model.required">

    // as different options arise,
    // i have more forks for each attribute combo

    <input class="form-control"
           type="text"
           ng-model="model" />
 </div>

有动态应用属性节点更好的办法?

Is there a better way to dynamic apply attributes to nodes?

推荐答案

我也很快建立了一个指令,它允许您指定动态属性。

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:

&LT; D​​IV DYN-ATTRS =someModelArray&GT;&LT; / DIV&GT;

和相应设置你的模型:

$scope.someModelArray = [
    { attr: 'myattribute', value: '' },
    { attr: 'anotherattribute', value: 'val' }
];

这篇关于动态属性与AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆