gpt4 book ai didi

angularjs - 指令 : how to evaluate ng-options in parent scope

转载 作者:行者123 更新时间:2023-12-03 06:54:13 25 4
gpt4 key购买 nike

我试图将 select 封装在自定义指令中,同时在指令声明中保留 ng-options 的使用。

目标是能够使用我的指令保留模板中 ng-options 的语法,但将其正确转发到指令内 select 的 ng-options。

不仅仅是文字,还有代码:

指令:

    angular.module('myApp').directive("selectField", function() {
return {
restrict: 'EA',
replace: true,
transclude : false,
templateUrl : "/common/tpl/form/select-field.html",
scope : {
label : "@",
model : "=",
options : "&"
}
};
});

模板:

<div class="form-group">
<label for="{{fieldId}}" class="col-lg-2 control-label">{{label | translate}}</label>
<div class="col-lg-10">
<select class="form-control" id="{{fieldId}}" ng-model="model" ng-options="{{options}}"></select>
</div>
</div>

用法:

 <select-field label="myLabel" model="data.mySelectValue" options="p.nom for p in myOptions"></select-field>

而且......错误:

Error: [$parse:syntax] Syntax Error: Token 'for' is an unexpected token at column 7 of the expression [p.nom for p in preteurs] starting at [for p in preteurs].

我尝试使用“&”、“=”和“@”属性等选项,但似乎没有任何效果。

使用“=”和“&”, Angular 爆炸会出现给定的错误,而使用“@”属性,表达式会在指令范围内求值,它可以工作,但不会渲染任何选项,因为指令的范围中不存在“myOptions”自己的范围...

我错过了什么吗?有办法做到这一点吗?

最佳答案

没有其他方法可以将选择列表和表达式作为单独的属性... http://blog.benkuhl.com/2013/08/how-to-use-ng-options-in-a-custom-directive-for-a-dropdown/

关于angularjs - 指令 : how to evaluate ng-options in parent scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19056434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com