select在ng-switch中不起作用。如果我从ng-switch中取出它,那么它将显示selItems值。
下面是JSFiddle中的情况
<div ng:app>
<div ng-controller='Ctrl'>
<div ng-switch on="controlType">
<div ng-switch-when="kendoDropDownList">
<select ng-model="selItems" ng-options="value for value in zoneList">Select Zone</select>
</div>
</div>
{{selItems}}
</div>
发布于 2015-06-13 00:28:20
这是因为ng-switch在每次执行时都会为它们生成一个独立作用域。
一个可能的解决方案是在select的模型中使用$parent:
<div ng:app>
<div ng-controller='Ctrl'>
<div ng-switch on="controlType">
<div ng-switch-when="kendoDropDownList">
<select ng-model="$parent.selItems" ng-options="value for value in zoneList">Select Zone</select>
</div>
{{selItems}}
</div>
</div>
</div>http://jsfiddle.net/2827vLLe/1/
发布于 2015-06-13 00:28:31
ng-switch-when创建一个独立的作用域,并且selItems值仅在该作用域中可用。有两种解决方案:
1.使用 $parent
<select ng-model="$parent.selItems" ng-options="value for value in zoneList">这告诉Angular将selItems添加到父作用域(Ctrl- scope )
语法2.使用 controller as
https://docs.angularjs.org/api/ng/directive/ngController
function Ctrl($scope)
{
this.controlType = "kendoDropDownList";
this.zoneList = ['rr','rrs','dsa'];
}
<div ng-controller='Ctrl as c'>
<div ng-switch on="c.controlType">
<div ng-switch-when="kendoDropDownList">
<select ng-model="c.selItems" ng-options="value for value in c.zoneList">Select Zone</select>
</div>
</div>
{{ selItems }}
</div>发布于 2015-06-13 00:33:38
试试这个,link.This现在工作得很好。
[1]: http://jsfiddle.net/2827vLLe/6/https://stackoverflow.com/questions/30807737
复制相似问题