Установить по умолчанию выбранную опцию - Angular JS - PullRequest
0 голосов
/ 22 ноября 2018

Я пытаюсь добавить выбранную по умолчанию опцию, если роль пользователя - «Поставщик-администратор».Ниже приведен код, над которым я работаю.

Когда я использую поставщика [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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...