为什么 ng-model 在 select return 字符串中而不是 JSON?
Why does ng-model in a select return string and not JSON?
我有以下代码:
<select class="form-control" id="existing-phases" ng-model="selectedPhase">
<option disabled selected value>-- select an option --</option>
<option ng-repeat="p in existingPhases" value="{{p}}">{{ p.Name }</option>
</select>
当我 select 我的 select 中的一个选项时,我希望得到对应于 selected 阶段的 JSON 应该是这样的:
{"ID":2,"Name":"Outlook"}
但是,出于某种原因,我将 JSON 作为字符串获取:
"{\"ID\":2,\"Name\":\"Outlook\"}"
如何使 ng-model returns 成为 JSON 而不是字符串?
P.S.: 我不想事后用方法转换它,因为我知道你可以用 JSON.Parse().
更新:
调试打印:
我用你的代码设置了一个 plunker,似乎工作正常
https://plnkr.co/edit/Q8Cmknqg8bGBc5hABtF1
<!DOCTYPE html>
<html>
<head>
<script data-require="angularjs@1.5.8" data-semver="1.5.8" src="https://opensource.keycdn.com/angularjs/1.5.8/angular.min.js"></script>
<script>
angular.module('app', [])
.controller('FooCtrl', function($scope) {
$scope.existingPhases = [
{"ID":1,"Name":"a"},
{"ID":2,"Name":"b"},
{"ID":3,"Name":"c"},
{"ID":4,"Name":"d"},
{"ID":5,"Name":"e"}];
$scope.selectedPhase;
});
</script>
</head>
<body ng-app="app">
<div ng-controller="FooCtrl">
<select id="existing-phases" ng-model="selectedPhase">
<option disabled selected value>-- select an option --</option>
<option ng-repeat="p in existingPhases" value="{{p}}">{{ p.Name }}</option>
</select>
{{selectedPhase}}
</div>
</body>
</html>
请检查您的代码,我认为这不是 select 指令问题
我以为我错了,但我没有。如果您使用 ng-repeat 而不是 ng-options,您的 ng-model 的 属性 将被设置为 JSON 字符串而不是实际的 javascript 对象。我发现这个问题可以更好地解释它:Angular UI Select2, why does ng-model get set as JSON string?.
所以为了解决我的问题,我不得不从 ng-repeat 更改为 ng-options。
我有以下代码:
<select class="form-control" id="existing-phases" ng-model="selectedPhase">
<option disabled selected value>-- select an option --</option>
<option ng-repeat="p in existingPhases" value="{{p}}">{{ p.Name }</option>
</select>
当我 select 我的 select 中的一个选项时,我希望得到对应于 selected 阶段的 JSON 应该是这样的:
{"ID":2,"Name":"Outlook"}
但是,出于某种原因,我将 JSON 作为字符串获取:
"{\"ID\":2,\"Name\":\"Outlook\"}"
如何使 ng-model returns 成为 JSON 而不是字符串?
P.S.: 我不想事后用方法转换它,因为我知道你可以用 JSON.Parse().
更新:
调试打印:
我用你的代码设置了一个 plunker,似乎工作正常
https://plnkr.co/edit/Q8Cmknqg8bGBc5hABtF1
<!DOCTYPE html>
<html>
<head>
<script data-require="angularjs@1.5.8" data-semver="1.5.8" src="https://opensource.keycdn.com/angularjs/1.5.8/angular.min.js"></script>
<script>
angular.module('app', [])
.controller('FooCtrl', function($scope) {
$scope.existingPhases = [
{"ID":1,"Name":"a"},
{"ID":2,"Name":"b"},
{"ID":3,"Name":"c"},
{"ID":4,"Name":"d"},
{"ID":5,"Name":"e"}];
$scope.selectedPhase;
});
</script>
</head>
<body ng-app="app">
<div ng-controller="FooCtrl">
<select id="existing-phases" ng-model="selectedPhase">
<option disabled selected value>-- select an option --</option>
<option ng-repeat="p in existingPhases" value="{{p}}">{{ p.Name }}</option>
</select>
{{selectedPhase}}
</div>
</body>
</html>
请检查您的代码,我认为这不是 select 指令问题
我以为我错了,但我没有。如果您使用 ng-repeat 而不是 ng-options,您的 ng-model 的 属性 将被设置为 JSON 字符串而不是实际的 javascript 对象。我发现这个问题可以更好地解释它:Angular UI Select2, why does ng-model get set as JSON string?.
所以为了解决我的问题,我不得不从 ng-repeat 更改为 ng-options。