设置默认选项-Angular JS

时间:2018-11-22 17:55:08

标签: html angularjs

如果用户角色为“ Supplier-Admin”,我尝试添加默认的选定选项。下面是我正在处理的代码。

当我使用供应商[0]或[1]等时,它可以工作。默认情况下,将选择该元素的公司名称。

但是当我使用供应商[currentCompany]时,没有选项没有被选择为默认值。

$scope.supplierRole = function() {

  var currentUser;
  if (localStorage.getItem("currentUser") !== null) {

    currentUser = JSON.parse(localStorage.getItem("currentUser"));
    console.log("Received", currentUser);
  } else {
    console.log("Not received");
  }
  var currentCompany = currentUser[0].company;
  if (currentUser[0].role == "Supplier-Admin") {
    $scope.newUser.company = $scope.supplier[currentCompany];
    return false;       
  } else if (currentUser[0].role == "Buyer-Admin") {
    return true;
  }
};
<div class="form-group" ng-hide="supplierRole()">
  <label class="control-label col-sm-2">Company</label>
  <div class="col-sm-10" ng-class="{ 'has-error' : addForm.addCompany.$invalid && !addForm.addCompany.$pristine }">
    <select class="form-control" 
            name="addCompany" placeholder="Select Company" 
            ng-options="company.name for company in supplier" 
            ng-model="newUser.company" 
            ng-required="true">
    </select>

    <span class="help-block"        
          ng-show="addForm.addCompany.$invalid && !addForm.addCompany.$pristine">
      Your Company is required.
     </span>
  </div>
</div>

0 个答案:

没有答案