2015-12-09 87 views
0

我下面的HTML無法使用角度js代碼設置選擇的選項?

<div class="form-group "> 
    <label class="col-sm-3 control-label">Role</label> 
    <div class=" col-sm-8"> 
     <select type="text" id="role" name="role" ng-model="role" class="form-control" required> 
      <option value="">Select Role</option> 
      <option ng-repeat="rol in rolelist" value="{{rol.id}}">{{rol.title}}</option> 
     </select> 
     <p class="ng-invalid" ng-show="addForm.role.$error.required">Role need to selected</p> 
    </div> 
</div> 

我想設置角色的值動態地點擊數據集更新的目的,設置DOM元素(對照)的值;裏面做控制器

$scope.data_set=function(id) 
     { 
      BlockUi(); 
      url=siteurl+'/admin/'+module+'/get-info'; 

       $http({ 
        url  : url, 
        method : "POST", 
        data : {"id":id} 

       }).then(function(responseText){ 
        data=responseText.data; 
        $scope.first_name=data.first_name; 
        $scope.user_id=data.id; 
        $scope.last_name=data.last_name; 
        $scope.user_name=data.user_name; 
        $scope.role=data.role; 

        $scope.email=data.email; 
        $scope.contact_number=data.contact; 
        $scope.image_file=data.image; 
        $scope.status=data.status; 

        UnblockUi(); 
       },function(error){ 
        UnblockUi(); 
        UnknownError(); 
        alert(JSON.stringify(error)); 
       }); 
     } 

上面的代碼工作,但所有作用模型,所以我不得不下面的代碼;我觀看並遵循其他問題的解決方案,但不適合我?

並且在此代碼後刪除ng必需的錯誤;

回答

0

感謝大家 事實上的努力我迷路了每一個地方,但解決方法很簡單;它與數據類型的問題;實際上選擇:角色模型包含字符串數據類型並始終;我將整數數據類型設置爲角色模型,這是錯誤的;我將json數據的數據類型轉換爲字符串,並將角色模型的值設置爲正常工作。

0

我可以建議你看看ng-options指令嗎?你會想要做的線沿線的東西:

<select ng-model="role" ng-options="rol as rol.Title for rol in rolelist"> 
    <option value="">Select Role</option> 
</select> 
+0

謝謝,我一遍又一遍地做到了;沒有變化 –

+0

當你使用我建議的代碼時,究竟是不是工作? – mkelley82

+0

不設置select的值;它顯示select中的角色列表,但不能動態設置其值。 –