我从服务中获取了一个json对象,并使用它的一些字段来填充我的选择选项列表。 当我尝试在控制器中打印所选值时,输出响应为"未定义"。
我哪里错了? JSON
[ {
"Accreditment" : {
"Id" : "1",
"Creator" : "John Smith",
"IdCreator" : "1",
"CreationDate" : "2014-07-01T18:13:51+02:00",
"CostCenter" : [ "5411-Channel1", "5412-Channel2" ],
"Destination" : [ "Playout Channel1", "Playout Channel2" ],
"IdUserEnabled" : [ "1", "2" ],
"WorkOrderType" : [ "New Asset", "Subtitling" ],
"StartDate" : "2013-05-04T18:13:51+02:00",
"EndDate" : "2014-10-04T18:13:51+02:00",
"Status" : "enabled"
}
} ]
HTML
<select class="form-control" ng-model="myOption" ng-change="selectAction()">
<option ng-repeat="cost in work.Accreditment.CostCenter" value="{{cost}}">{{cost}}</option>
</select>
CONTROLLER
mwm3.controller('CreateWorkOrderCtrl',function($scope){
$scope.selectAction=function(){
console.log($scope.myOption);
};
});
答案 0 :(得分:0)
问题在于你的JSON。
只需删除JSON数组即可。它会很好。
详细了解JSON JSON是一个对象而不是数组!
校正:
$ scope.work = { &#34; Accreditment&#34; :{ &#34;标识&#34; :&#34; 1&#34;, &#34;创建者&#34; :&#34; John Smith&#34;, &#34; IdCreator&#34; :&#34; 1&#34;, &#34; CreationDate&#34; :&#34; 2014-07-01T18:13:51 + 02:00&#34;, &#34; CostCenter&#34; :[&#34; 5411-Channel1&#34;,&#34; 5412-Channel2&#34; ] &#34;目的地&#34; :[&#34; Playout Channel1&#34;,&#34; Playout Channel2&#34; ] &#34; IdUserEnabled&#34; :[&#34; 1&#34;,&#34; 2&#34; ] &#34; WorkOrderType&#34; :[&#34; New Asset&#34;,&#34; Subtitling&#34; ] &#34;起始日期&#34; :&#34; 2013-05-04T18:13:51 + 02:00&#34;, &#34;结束日期&#34; :&#34; 2014-10-04T18:13:51 + 02:00&#34;, &#34;状态&#34; :&#34;启用&#34; } }
答案 1 :(得分:0)
我添加了ng-selected。试试这个。
<select class="form-control" ng-model="myOption" ng-change="selectAction()">
<option ng-repeat="cost in work.Accreditment.CostCenter" value="{{cost}}"
ng-selected="cost==myOption">
{{cost}}
</option>
</select>
希望这会有所帮助.....
答案 2 :(得分:-1)
正如我从您的标签中看到的那样,您正在使用angularjs。 Angular有自己的select标签指令。它是这样的:
<select ng-model="myCost" ng-options="cost in work.Accreditment.CostCenter">
<option value="">-- choose cost --</option>
</select>
并且,是的,从json中删除数组括号。在这种情况下,您正在遍历一个对象。不需要阵列。
答案 3 :(得分:-1)
为什么不像this那样做?
<select ng-model="selectedItem" ng-options="item as item.Accreditment.Creator for item in list"></select>