2016-07-25 74 views
0

我仍然是新的角度,我不知道如何使表單,當我想更新用戶填寫表單與他自己的數據。我有控制器這樣的:角度設置表單數據爲所有字段

 // take data for organization from database    
     dataService.allOrganization().then(function (data) { 
      vm.allOrg = data; 
     }); 
     // take data for user from database 
     dataService.getOneUser(theId).then(function(data) { 
      vm.oneUserUpdate = data; 

     }); 

     $scope.user = {}; 
     $scope.submitForm = function(theId) { 
      $scope.user.idUser = theId; 
      dataService.updateUserPost($scope.user).then(function (data) { 
       vm.oneUserInfo = data;//response from http request 
      }) 
     } 

,並考慮:

<form name="userForm" ng-submit="submitForm(userDataCtr.oneUserUpdate.identities[0].user_id)"> 
      <div class="form-group"> 
       <label>Encryption Key :</label> 
       <input type="text" class="form-control" name="encryption" ng-model="user.encryption" > 
      </div> 
      <div class="form-group"> 
       <label>Admin :</label> 
       <select class="form-control" name="admin" ng-model="user.admin"> 
        <option>Select...</option> 
        <option value="true">True</option> 
        <option value="false">False</option> 
       </select> 
      </div> 
      <div class="form-group"> 
       <label>Organization ID:</label> 
       <select class="form-control" name="organization" ng-model="user.organization"> 
        <option>Select...</option> 
        <option ng-repeat="org in userDataCtr.allOrg" value="{{org.id_org}}"> 
         {{org.name_org}} 
        </option> 
       </select> 
      </div> 
      <div class="form-group"> 
       <label></label> 
       <div class="checkbox"> 
        <label><input type="checkbox" value="1" name="role_cro" ng-model="user.role_cro">ROLE_CRO</label> 
       </div> 
       <div class="checkbox"> 
        <label><input type="checkbox" value="1" name="role_client" ng-model="user.role_client">ROLE_CLIENT</label> 
       </div> 
      </div> 
      <button type="submit" class="btn btn-primary">Submit</button> 
     </form> 

又如何,如果有可能,以填補用戶數據,輸入字段中的所有數據具有價值,複選框,如果被檢查他們需要,選擇組織選擇等。

謝謝。

+0

您希望用戶在提交前填寫所有字段?你想驗證正確嗎? –

+0

不,我希望字段在您想要更新用戶時自動填充,然後在需要時更改數據。 – Sasa

回答

0

您必須更新模型。例如

dataService.getOneUser(theId).then(function(data) { 
     vm.user={}; 
     vm.user.encryption = data.encryption; 
     vm.user.role_client = data.role; 
    }); 

上查看

<div ng-controller="Mycontroller as my"> 
<div class="form-group"> 
       <label>Encryption Key :</label> 
       <input type="text" class="form-control" name="encryption" ng-model="my.user.encryption" > 
</div> 
</div> 

等等......你在你的控制器實例上使用您的視圖。

+0

我改變了,但是沒有定義$ scope的錯誤。 – Sasa

+0

我想你不使用$範圍,你使用var vm = this ?,如果是這樣,用代碼中的vm替換$ scope,我也要編輯它。 – urvashi

+0

我正在使用這兩個,我所使用的所有數據vm,只用於表單驗證我使用$ scope。 – Sasa

0

無論何時您想使用模板內的控制器(或指令)中的數據,都必須將其綁定到控制器的$ scope。

.controller('myFormController', function() { 

    dataService.allOrganization().then(function (data) { 
     // Populate our scope the required data 
     $scope.allOrg = data; 
    }); 
}); 

現在我們可以使用ng-model將這些變量綁定到我們的表單域。
<input type="text" name="firstName" ng-model="allOrg.firstName">

並非所有的表單元素都表現相同。您應該查看角度文檔以獲取更多深入的表單綁定示例。
Angular form documentation

+0

但如果我改變模型的名稱如何從表單提交所有數據?我爲ng-model user.etc命名,並以這種方式獲取所有數據。 – Sasa

相關問題