角度条件式多选指令 [英] Conditional multi-select directive in angular
问题描述
我正在创建一个选择框指令,作为该指令的一部分,我需要指定选择是否为多个.
I am creating a select box directive where, as part of it, I need to specify if the select is multiple or not.
我已尝试在我的一个范围对象中将"multiple"属性设置为"multiple"值,假设该属性将执行并在我的选择框中设置multiple="multiple"
,如下所示:
I have tried to set a multiple property in one of my scope objects a value of "multiple" assuming that it shall execute and set multiple="multiple"
on my selectbox as follows:
<select multiple="{{properties.multiple}}" /*...(other working properties)...*/ ></select>
但这给我带来了一个错误.
But this got me an error..
Error: [$compile:selmulti] http://errors.angularjs.org/1.4.4/$compile/selmulti?p0=%3Cselect%20id%3D%22…y%20as%20value%20for%20(key%20%2C%20value)%20in%20properties.options%22%3E
at Error (native)
at path/to/angular/angular-1.4.4/angular.min.js:6:416
at X (path/to/angular/angular-1.4.4/angular.min.js:71:93)
at ha (path/to/angular/angular-1.4.4/angular.min.js:56:379)
at S (path/to/angular/angular-1.4.4/angular.min.js:54:425)
at path/to/angular/angular-1.4.4/angular.min.js:68:209
at path/to/angular/angular-1.4.4/angular.min.js:118:217
at n.a.$get.n.$eval (path/to/angular/angular-1.4.4/angular.min.js:133:39)
at n.a.$get.n.$digest (path/to/angular/angular-1.4.4/angular.min.js:130:60)
at n.scopePrototype.$digest (chrome-extension://ighdmehidhipcmcojjgiloacoafjmpfk/dist/hint.js:1955:23)
我可以使用ng-if
,但是我试图找到一种比制作冗余选择标签更好地区分标准标签和多个标签的方法.
I can use ng-if
but am trying to find a better approach than making redundant select tags to differentiate between standard and multiple.
有什么更好的方法?
推荐答案
创建使用ng-if
的自己的指令,或编写具有逻辑的指令以在单个和多个之间切换.看起来angular不想这样做,但是如果它对您有用,那就去做吧.
Create your own directive that use ng-if
s or write a directive with logic to switch between single and multiple. Looks like angular didn't want to do that but if it works for you, go for it.
这篇关于角度条件式多选指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!