angular 中的条件多 select 指令
Conditional multi-select directive in angular
我正在创建一个 select 框指令,作为其中的一部分,我需要指定 select 是否为多个。
我试图在我的一个范围对象中设置一个倍数 属性 值 "multiple" 假设它将执行并在我的 select 上设置 multiple="multiple"
方框如下:
<select multiple="{{properties.multiple}}" /*...(other working properties)...*/ ></select>
但这让我出错了..
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
,但我正在尝试找到一种比制作冗余 select 标签更好的方法来区分标准标签和多个标签。
有什么更好的方法?
创建您自己的使用 ng-if
的指令或编写一个指令,其中包含在单个和多个之间切换的逻辑。看起来 angular 不想那样做,但如果它适合你,那就去做吧。
一种可能是在您的指令定义中添加一个 link 函数来检查范围外的 properties.multiple 值,然后使用 jquery 或 jqLite 来设置该属性。所以:
angular.directive("yourDirective", function() { return {
link: function(scope, element, attrs) {
if (scope.properties.multiple) {
elements.children("select").attr("multiple",true);
}
},
templateUrl: "yourtemplate.html", ...
}
});
我正在创建一个 select 框指令,作为其中的一部分,我需要指定 select 是否为多个。
我试图在我的一个范围对象中设置一个倍数 属性 值 "multiple" 假设它将执行并在我的 select 上设置 multiple="multiple"
方框如下:
<select multiple="{{properties.multiple}}" /*...(other working properties)...*/ ></select>
但这让我出错了..
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
,但我正在尝试找到一种比制作冗余 select 标签更好的方法来区分标准标签和多个标签。
有什么更好的方法?
创建您自己的使用 ng-if
的指令或编写一个指令,其中包含在单个和多个之间切换的逻辑。看起来 angular 不想那样做,但如果它适合你,那就去做吧。
一种可能是在您的指令定义中添加一个 link 函数来检查范围外的 properties.multiple 值,然后使用 jquery 或 jqLite 来设置该属性。所以:
angular.directive("yourDirective", function() { return {
link: function(scope, element, attrs) {
if (scope.properties.multiple) {
elements.children("select").attr("multiple",true);
}
},
templateUrl: "yourtemplate.html", ...
}
});