2015-11-11 22 views
0

在AngularJS中 - 我有如下的下拉代碼。 type.entityTypeId字段是一個長數據類型。下拉菜單在創建頁面中正常工作,並且可以將'type.entityTypeId'字段插入到數據庫中。Angularjs - 下拉式 - 如果鍵/值對始終是字符串數據類型

但是,在編輯頁面中使用相同代碼重新加載相同數據時,它不起作用。

是這個問題,因爲我有數據類型爲長?

我有其他下拉工作正常,其中我有所有的字符串數據類型。

   <div class="form-group"> 
       <label class="control-label col-md-2 required">ENTITY TYPE</label> 
       <div class="col-md-2"> 
        <select name="entityType" class="form-control" ng-model="entity.entityType.entityTypeId" 
         required="required"> 
         <option value="">Please select</option> 
         <option ng-repeat="type in entityTypes" value="{{type.entityTypeId}}">{{type.entityTypeName}}</option> 
        </select> 
       </div> 
       <span style="color: red" ng-show="entityAddForm.entityType.$invalid"> <span 
        ng-show="entityAddForm.entityType.$error.required">Entity type is required.</span> 
       </span> 
      </div> 

更新:

這是用來加載下拉數據的JSON。你可以看到entityTypeId是一個數字。在其他情況下,如果id是字符串,則它可以工作。

[ 
{ 
entityTypeId: 3, 
entityTypeName: "Branch of Legal Entity" 
}, 
{ 
entityTypeId: 1, 
entityTypeName: "Legal Entity" 
}, 
{ 
entityTypeId: 2, 
entityTypeName: "Notional Entity" 
} 
] 

回答

1

使用數字不應該是一個問題。我知道文檔只使用字符串,但我可以使用相同的使用數字。

<div ng-app="myApp" ng-controller="myController"> 
    <select ng-model="entity.selected"> 
    <option value="{{ent.entityTypeId}}" ng-repeat="ent in entityTypes">{{ent.entityTypeName}}</option> 
    </select> 
    Selected entity id {{entity.selected}} 
</div> 

angular.module("myApp", []) 
    .controller("myController", ["$scope", function ($scope) { 
    $scope.entityTypes = [{ 
     entityTypeId: 3, 
     entityTypeName: "Branch of Legal Entity" 
    }, { 
     entityTypeId: 1, 
     entityTypeName: "Legal Entity" 
    }, { 
     entityTypeId: 2, 
     entityTypeName: "Notional Entity" 
    }]; 
    $scope.entity = { 
     selected: 3 
    }; 
    }]); 

JSFiddle

相關問題