我使用 angularjs 并选择选项集合。如果我在 html 选项标签中添加一切正常。但是,如果我尝试从 Angular 添加项目,则它不起作用。
我的代码:
<div ng-controller="statisticsTableController">
<select ng-model="publishersdata"
ng-options="s for s in publishersdata"
chosen option="publishersdata">
</select>
</div>
和 AngularJs 代码:
function statisticsTableController($scope, $http) {
var publishersArray = [];
$http.get('/api/getAllItems').success(function(data) {
angular.forEach(data, function(data) {
publishersArray.push(data.Name);
});
});
this.publishersdata = publishersArray;
//...
}
为什么它不起作用?以及如何解决?
更新
修改后:
function statisticsTableController($scope, $http) {
var publishersArray = [];
$http.get('/api/getAllItems')
.success(function (data) {
angular.forEach(data, function (data) {
publishersArray.push(data.Name);
});
});
$scope.publishersdata = publishersArray;
//...
}
HTML:
<div ng-controller="statisticsTableController">
<select multiple
ng-model="publishersdata"
ng-options="s for s in publishersdata"
chosen option="publishersdata">
</select>
</div>
无法正常工作
最佳答案
我看到的一个问题是您的 ng-model
和 ng-options
都指向同一个 publishersdata
对象,这可能是刹车的原因之一。您的 ng-model
应该是一个对象,其中包含在下拉列表中选择的内容。
同样如上所述,您应该将 publishersdata
附加到 $scope
,否则将无法在 HTML 中访问
除此之外,我觉得还不错。
关于javascript - AngularJs 选择添加选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30253732/