angularjs中的条件ng模型绑定 [英] Conditional ng-model binding in angularjs

查看:63
本文介绍了angularjs中的条件ng模型绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,它是一个需要处理复杂和简单元素的数组:

I have a model which is an array that needs to handle complex and simple elements :

{
    "object" :[
               "element1.html",
               "element2.html",
               {
                   "url:"element3.html",
                   "title" : "Title 3"
               },
               "element4.html",
               "element5.html"
            ]
}

有没有办法制作一个可以处理angularjs中的简单元素和复杂元素的select(显示复杂的url)?

Is there some way to make a select which can handle both simple and complex elements in angularjs (showing url from the complexs)?

方法1

我的意思是这样的:

ng-model="(object[$index].url ? object[$index].url : object[$index])"

方法2

制作一个标准化对象将使用空数组和指示w的文件类型为每个对象提供复杂的结构hether是简单还是复杂。

Make a normalized object that will have the complex structure for each object with empty arrays and a file type indicating whether is simple or complex.

我可以用角度来制作一些魔法来避免第二种方法吗?

Could I make some magic with angular to avoid the second approach?

谢谢!

推荐答案

您可以使用 ngSwitch ngIf 然后放置正确的元素。

You could use ngSwitch or ngIf and place the correct element then.

以ngIf为例:

<input ngIf="object[$index].url" ngModel="object[$index].url">
<input ngIf="!object[$index].url" ngModel="object[$index]">

如果条件不满足,angular将完全删除dom元素,直到条件满足为止。

If the condition is not met, angular will completely remove the dom element, till the condition will meet.

这篇关于angularjs中的条件ng模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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