【发布时间】:2016-11-06 04:36:29
【问题描述】:
此处显示了用于填充基于移动操作系统选择的常见手机和平板电脑型号列表的应用程序的一部分:
<div class="input-field col s3">
<label class="active">Environment (OS)</label>
<select ng-model="environment" ng-change="listBrowsers()">
<option value="" disabled>Select an OS</option>
<option ng-repeat="OS in OSes">{{OS}}</option>
</select>
</div>
还有一个 $scope.listBrowsers() 的 sn-p 用于上下文(来自控制器)......
$scope.listBrowsers = function() {
$scope.browsers = MachineDataService.getBrowsers($scope.environment);
$scope.browser1 = null;
$scope.notMobile(); //calls another function to determine mobile-ness
$scope.getMobileDevices($scope.environment);
return $scope.browsers;
}
接下来,“getMobileDevices”作用域函数调用一个类似命名的服务方法:
this.getMobileDevices = function(envt) {
var deviceList = [];
for (i=0; i < _devices.length; i++) {
if (_devices[i].versions.indexOf(envt) > -1) {
deviceList.push(_devices[i].name);
}
}
return deviceList;
}
但是问题在于视图的结果变化:
这行得通--
<div class="input-field col s6">
<label class="active">Phone/Tablet Model:</label>
<p ng-repeat="device in mobileDevices">{{device}}</p></div>
这不是:
<div class="input-field col s6><select ng-model="mobileDeviceType">
<option value="" disabled selected>Choose a device...</option>
<option ng-repeat="device in mobileDevices" value="{{device}}">{{device}}</option>
</select></div>
事实上,即使用单选按钮替换选择也是有效的——来自同一来源!
注意我承认我可能遗漏了一些东西——Angular 对我来说还是有点新鲜。请原谅使用 Materialize 而不是 Angular Material——我可能稍后会重写以使用 Angular Material,但一开始并不知道它的存在。
编辑——为了帮助说明这一点,这里是使用中继器的段落的结果截图,而使用 ng-options 的选择。
【问题讨论】:
-
Option 元素使用 ng-options。 docs.angularjs.org/api/ng/directive/ngOptions
-
我知道这是首选方式,但奇怪的是它也失败了,而且我已经在同一个应用程序的其他地方成功使用了它。这里应该是
标签: javascript angularjs material-design