从JSON AngularJs NG-模型绑定 [英] 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>
在角我得到一个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-模式=produk.category
,但它适用于&LT; P&GT;选择:{{produk.category}}&LT; / p&GT;
In view I can't assign the json data to ng-model="produk.category"
but it works for <p>Selected: {{produk.category}}</p>
这是返回的JSON 对象{类别:'旅游'}
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>
从code你有一个选择是 produk.category
的值。里面只有字符串旅游
。而在Javascript中的字符串没有财产叫名字。
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>
将解决(因为现在你打印的字符串,而不是一个不存在的属性称为名
在你的字符串)你的问题。
will solve your problems (since now you are printing the string, not a non-existing property called "name"
in your string).
这篇关于从JSON AngularJs NG-模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!