[英]Angularjs select with ng-options calling a function?
这是我的html
<select ng-model="selectedMarker" ng-options="shape.text for shape in Selects('shapes')">
</select>
和代码
angular.module('todo', ['ionic'])
.factory('Projects', function ($http) {
return {
options: function (type) {
var selects = [{
type: "shapes",
list: [{
text: "Circle",
value: "S"
}, {
text: "Polygon",
value: "P"
}]
}];
var _filter = selects.filter(function (item) {
if (item.type === type) {
return item.list;
}
});
return _filter;
},
})
.controller('TodoCtrl', function ($scope, $timeout, $ionicModal, Projects, $ionicSideMenuDelegate) {
$scope.map = null;
$scope.Selects = function (type) {
var x= Projects.options(type);
return x;
}
});
这导致呈现一个空的选择。 我将$ scope.Selects替换为数组而不是调用函数,但是随后填充了select。 为什么ng-options无法与函数一起使用? 我的选择标签有什么问题吗?
您的实现是错误的(更不用说过于复杂了)。
options
函数返回原始的selects
数组,而不是shapes
数组。
目前尚不清楚您到底想达到什么目的,但是如果您更改var xx = _filter[0];
到var xx = _filter[0][type];
它应该工作。
我强烈建议您仔细查看正在使用的功能(例如filter
),因为您可能会感到惊讶……
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.