使用 JSON 中的响应中的 AngularJS 填充 select 列表
Populate a select list with AngularJS from a response in JSON
我想在 JSON 的服务器上下载一个响应,其中包含用于填充我的 select 列表的属性。我想用 AngularJS 来做,我正在使用 Angular 2。
没有出现,我认为问题出在我的属性 ng-repeat 上:
<div id="myDiv">
<div ng-app="d3DemoApp">
<div ng-controller="AppCtrl">
<div ng-repeat="n in filters track by $index">
{{n}}
</div>
</div>
</div>
</div>
这是我的控制器:
angular.module('d3DemoApp',[])
.controller('myCtrl',function($scope) {
$scope.notes = userService.getData();
//Create and append select list
var selectList = document.createElement("select");
selectList.setAttribute("id", "releaseFilter");
myDiv.appendChild(selectList);
selectList.setAttribute("class", "form-control");
selectList.setAttribute("onclick", "myFunction()");
//Create and append the options
for (var i = 0; i < $scope.notes.length; i++) {
var option = document.createElement("option");
option.setAttribute("value", array[i]);
option.text = $scope.notes[i];
selectList.appendChild(option);
}
});
这是应该下载响应的服务:
app.service("userService",["$http",
function($http) {
_this = this;
this.getData = function() {
},
$http.get('./dataOnServer.json'). // This adress is normally an HTTP adress which send me the JSON
success(function(data) {
return data;
});
}
]);
这是 Plunker 问题的在线示例:https://plnkr.co/edit/du7sU8bhg2G3X7HckbV9?p=preview
希望大家能帮帮我,非常感谢!
我要指出的是,当您没有在您的范围或任何地方定义此类变量时,您是在重复 filters
吗?您可能应该重复 $scope.notes
,这样它就会变成这样:
<div id="myDiv">
<div ng-app="d3DemoApp">
<div ng-controller="AppCtrl">
<div ng-repeat="n in notes track by $index">
{{n}}
</div>
</div>
</div>
</div>
编辑:
你可以这样 select:
<select>
<option ng-repeat="n in notes">{{n.value}}</option>
</select>
并且您的JSON无效。重复应该是这样的:
[{value: "value 1"},{value: "value 2"},{value: "value 3"}]
我想在 JSON 的服务器上下载一个响应,其中包含用于填充我的 select 列表的属性。我想用 AngularJS 来做,我正在使用 Angular 2。 没有出现,我认为问题出在我的属性 ng-repeat 上:
<div id="myDiv">
<div ng-app="d3DemoApp">
<div ng-controller="AppCtrl">
<div ng-repeat="n in filters track by $index">
{{n}}
</div>
</div>
</div>
</div>
这是我的控制器:
angular.module('d3DemoApp',[])
.controller('myCtrl',function($scope) {
$scope.notes = userService.getData();
//Create and append select list
var selectList = document.createElement("select");
selectList.setAttribute("id", "releaseFilter");
myDiv.appendChild(selectList);
selectList.setAttribute("class", "form-control");
selectList.setAttribute("onclick", "myFunction()");
//Create and append the options
for (var i = 0; i < $scope.notes.length; i++) {
var option = document.createElement("option");
option.setAttribute("value", array[i]);
option.text = $scope.notes[i];
selectList.appendChild(option);
}
});
这是应该下载响应的服务:
app.service("userService",["$http",
function($http) {
_this = this;
this.getData = function() {
},
$http.get('./dataOnServer.json'). // This adress is normally an HTTP adress which send me the JSON
success(function(data) {
return data;
});
}
]);
这是 Plunker 问题的在线示例:https://plnkr.co/edit/du7sU8bhg2G3X7HckbV9?p=preview
希望大家能帮帮我,非常感谢!
我要指出的是,当您没有在您的范围或任何地方定义此类变量时,您是在重复 filters
吗?您可能应该重复 $scope.notes
,这样它就会变成这样:
<div id="myDiv">
<div ng-app="d3DemoApp">
<div ng-controller="AppCtrl">
<div ng-repeat="n in notes track by $index">
{{n}}
</div>
</div>
</div>
</div>
编辑:
你可以这样 select:
<select>
<option ng-repeat="n in notes">{{n.value}}</option>
</select>
并且您的JSON无效。重复应该是这样的:
[{value: "value 1"},{value: "value 2"},{value: "value 3"}]