2014-04-11 41 views
0

我需要显示当前位置的记录或当前员工哪来的库存是目前放在NG模型两个值在Angularjs

<select class=" form-control" ng-model="location.name"> 
     <option value="">Choose Location</option> 
    <optgroup label="Locations"> 
     <option value="{{location.resource_uri}}" ng-repeat="location in locations">{{location.name}}</option> 
    </optgroup> 
    <optgroup label="Holder"> 
     <option value="{{user.resource_uri}}" ng-repeat="user in users">{{user.first_name + ' ' + user.last_name}}</option> 
    </optgroup> 
</select> 

我可以把NG-模型两个值,因此,如果位置不空,我得到当前位置,如果位置为空,我会选择员工姓名。

+0

我创建了一个jsfiddle .. [JSFIDDLE](http://jsfiddle.net/TFmjQ/6/)在这个jsfiddle中告诉我你需要什么 –

+0

所以,如果我选择Holder:它会改变Holder名称并把位置为空,和其他方式 – Erick

+0

我没有得到你........... –

回答

0

看看这个

Working Demo

HTML

<div class="form-control" ng-app="main" ng-controller="Controller"> 
    <select ng-model="category"> 
     <optgroup ng-repeat="category in categories" label="{{ category.name }}"> 
      <option ng-repeat="subCat in category.items"> 
       <span ng-switch="category.name"> 
        <span ng-switch-when="Location"> 
         {{ subCat.name }} 
        </span> 
        <span ng-switch-when="Holder"> 
         {{subCat.first_name+" "+subCat.last_name}} 
        </span> 
       </span> 
      </option> 
     </optgroup> 
    </select> 

    {{category}} 
</div> 

脚本

var main = angular.module('main', []); 
// Main Controller 
main.controller('Controller', function ($scope) { 
    $scope.categories = [{ 
     id: 5, 
     name: "Locations", 
     items: [{ 
      resource_uri: 'ABC', 
      name: "ABC-Name" 
     }, { 
      resource_uri: 'XYZ', 
      name: "XYZ-Name" 
     }, { 
      resource_uri: 'LMN', 
      name: "LMN-Name" 
     }, { 
      resource_uri: 'PQR', 
      name: "PQR-Name" 
     }] 
    }, { 
     id: 17, 
     name: "Holder", 
     items: [{ 
      first_name: 'Manu', 
      last_name: 'Mathew' 
     }, { 
      first_name: 'Sunny', 
      last_name: 'Mathew' 
     }, { 
      first_name: 'Jacob', 
      last_name: 'Mathew' 
     }, { 
      first_name: 'Vijay', 
      last_name: 'Mathew' 
     }] 
    }]; 
}); 
+0

我将有2 ng重复,因为我从两个不同的api获取数据,所以我将需要两个ng模型... – Erick

+0

确定其工作正常....正确.... –