0

我從服務中獲取json對象,並使用它的一些字段填充我的選擇選項列表。 當我嘗試在我的控制器中打印選定的值時,輸出響應爲「未定義」。Angular.js選擇選項而不使用ng-option指令

我在哪裏錯了? 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> 

控制器

mwm3.controller('CreateWorkOrderCtrl',function($scope){ 
    $scope.selectAction=function(){ 
     console.log($scope.myOption); 
    }; 
}); 
+0

是什麼'work'爲NG-重複,你得到填充選擇值? – Mritunjay

回答

0

的問題是與你的JSON。

只需刪除JSON數組。它會正常工作。

閱讀更多關於JSON JSON是一個不是數組的對象!

修正:

$ scope.work = { 「Accreditment」:{ 「ID」: 「1」, 「造物主」: 「約翰·史密斯」, 「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「, 「結束日期」: 「2014-10-04T18:13:51 + 02:00」, 「狀態」: 「已啓用」 }}

-1

當我從你的代碼中看到,您正在使用angularjs。 Angular對select標籤有自己的指令。它是這樣的:

<select ng-model="myCost" ng-options="cost in work.Accreditment.CostCenter"> 
     <option value="">-- choose cost --</option> 
    </select> 

而且,是的,從您的json刪除數組括號。在這種情況下,你正在遍歷一個對象。不需要數組。

+0

介意解釋downvote? – zszep

-1

爲什麼不像this那樣做?

<select ng-model="selectedItem" ng-options="item as item.Accreditment.Creator for item in list"></select> 
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> 

希望這有助於.....

相關問題