之前我经常搜索过这个问题,但我找不到任何东西。问题是我在服务器的GET请求之后收到一个JSON,并且正确接收它(我正在检查这个)但是选择选项没有重新加载信息。
这是我的代码:
angular.module('app',[])
.controller('SelectController', ['$scope','$http', function($scope, $http) {
$scope.data_info = [
{id:1, name:'Option A'},
{id:2, name:'Option B'},
{id:3, name:'Option C'}
];
$scope.data_model = null;
$http.get('http://localhost:7800/data_info'{headers: {'Content-Type': 'application/json'}})
.success(function(data) {
$scope.data_info = data;
})
.error(function(data){
});
};
}]);
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body ng-app="app">
<div ng-controller="SelectController">
<select
ng-options="d as d.name for d in data_info track by d.id"
ng-model="data_model">
<option value="" disabled selected>Choose an option</option>
</select>
model: {{data_model}}
</div>
</body>
</html>
对此有何解决方案?
答案 0 :(得分:0)
您需要进行一些更改。以下代码可以使用:
替换你的控制器:
$scope.data.model = {};
有:
$scope.selectedInfo = null;
$scope.data.info = {};
然后在.success:
.success(function(data) {
$scope.data.info = data;
})
在您的HTML上:
<select ng-model="selectedInfo"
ng-options="item as item.name for item in info">
<option value="" disabled selected>Choose an option</option>
</select>
答案 1 :(得分:0)
$scope.data
将是未定义的。像$scope.data ={info:{}, model:{}};
答案 2 :(得分:0)
问题是,对于materialize.css, select 标记已被覆盖。
将class="browser-default"
添加到选择标记。