来自 json 的 AngularJs ng-model 绑定 [英] AngularJs ng-model bind from json
问题描述
我有以下下拉菜单:
<h3>Selectize theme</h3>
<p>Selected: {{produk.category}}</p>
<ui-select ng-model="produk.category" theme="selectize" ng-disabled="disabled" style="width: 300px;">
<ui-select-match >{{$select.selected.name}}</ui-select-match>
<ui-select-choices repeat="cat in categories | filter: $select.search">
<span ng-bind-html="cat.name | highlight: $select.search"></span>
</ui-select-choices>
</ui-select>
在 angular 中我得到一个 json 格式的数据:
In angular I get a data in json format:
$scope.getProductToEdit = function(id){
Account.getProductToEdit(id)
.then(function(response){
$scope.produk = response.data.product;
//console.log($scope.produk); ---> return json
return $scope.produk;
})
.catch(function(response){
})
}
if($stateParams.id){
$scope.getProductToEdit($stateParams.id);
}
鉴于我无法将 json 数据分配给 ng-model="produk.category"
但它适用于 <p>Selected: {{produk.category}}
In view I can't assign the json data to ng-model="produk.category"
but it works for <p>Selected: {{produk.category}}</p>
这是json返回的内容Object {category: 'Tours'}
This is what returned by json Object {category: 'Tours'}
谢谢!!
推荐答案
您面临的问题是您试图读取模型中不存在的属性.特别是在这一行:
The problem you are facing is that you are trying to read a property in your model that doesn't exist. Particularly in this line:
<ui-select-match >{{$select.selected.name}}</ui-select-match>
从代码中您选择的值为 produk.category
.里面只有字符串"Tours"
.Javascript 中的字符串没有名为 name 的属性.
From the code you have the value that is selected is produk.category
. Inside there there is only the string "Tours"
. And an string in Javascript has no property called name.
AngularJS 的正常行为是在属性不存在时忽略.所以你什么也得不到.改成这样:
AngularJS normal behavior is to ignore when properties don't exist. So you get nothing. Changing it to this:
<ui-select-match >{{$select.selected}}</ui-select-match>
将解决您的问题(因为现在您打印的是字符串,而不是字符串中名为 "name"
的不存在的属性).
will solve your problems (since now you are printing the string, not a non-existing property called "name"
in your string).
这篇关于来自 json 的 AngularJs ng-model 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!