Angular-UI-Select ng-model 不使用 $scope 上的简单变量

Angular-UI-Select ng-model not working with a simple variable on $scope

如何清除具有 select 值的数组,以便值可以 return 到 select?

我有一个人物数组。 people 数组值在 select 中可用。当我选择名字时,它们被转移到 multipleDemo 数组。你不能从 select 中重新 select 它们,因为它们消失并移动到 multipleDemo 数组。使用 Delete 按钮,我必须将 multipleDemo 数组中的所有元素(第一个元素除外)删除到 people 数组中。这样您就可以再次从 select 中选择一个名称。函数错误 $clearTag.

预期行为: 示例:

  1. Select:弗拉基米尔
  2. 出现标签弗拉基米尔
  3. Select Wladimir(您不能选择 Wladimir,因为他已经被选中)
  4. 点击删除。用multipleDemo数组切割元素(标签)放入数组people
  5. 你可以再次select弗拉基米尔

这是我的代码:http://plnkr.co/edit/TPZjXkkSRrIc5ApzP07F?p=preview

index.html

<!DOCTYPE html>
<html lang="en" ng-app="demo">
<head>
  <meta charset="utf-8">
  <title>AngularJS ui-select</title>

  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular-sanitize.js"></script>
  <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.css">

  <!-- ui-select files -->
  <script src="select.js"></script>
  <link rel="stylesheet" href="select.css">

  <script src="demo.js"></script>

  <!-- Select2 theme -->
  <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/select2/3.4.5/select2.css">
  <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.8.5/css/selectize.default.css">

  <style>
    body {
      padding: 15px;
    }

    .select2 > .select2-choice.ui-select-match {
      /* Because of the inclusion of Bootstrap */
      height: 29px;
    }

    .selectize-control > .selectize-dropdown {
      top: 36px;
    }
  </style>
</head>
<body ng-controller="DemoCtrl">
    
  <h3>Array of strings</h3>
  <button ng-click='clearTag()'>Delete</button>
  <ui-select tagging tagging-label="new tag" multiple ng-model="multipleDemo" 
  on-select="OnClickSelect($item)" on-remove="OnRemoveSelect($item)"
  theme="select2" ng-disabled="disabled" style="width: 300px;">
    <ui-select-match placeholder="Select name...">{{$item.name}}</ui-select-match>
    <ui-select-choices  repeat="item in people | filter:$select.search">
      {{item.name}}
    </ui-select-choices>
  </ui-select>
  <p>Selected: {{multipleDemo}}</p>
  <hr>  

</body>
</html>

demo.js

app.controller('DemoCtrl', function($scope, $http, $timeout) {
  $scope.multipleDemo =[];
    $scope.people = [
    { name: 'Adam',      email: 'adam@email.com',      age: 12, country: 'United States' },
    { name: 'Amalie',    email: 'amalie@email.com',    age: 12, country: 'Argentina' },
    { name: 'Estefanía', email: 'estefania@email.com', age: 21, country: 'Argentina' },
    { name: 'Adrian',    email: 'adrian@email.com',    age: 21, country: 'Ecuador' },
    { name: 'Wladimir',  email: 'wladimir@email.com',  age: 30, country: 'Ecuador' },
    { name: 'Samantha',  email: 'samantha@email.com',  age: 30, country: 'United States' },
    { name: 'Nicole',    email: 'nicole@email.com',    age: 43, country: 'Colombia' },
    { name: 'Natasha',   email: 'natasha@email.com',   age: 54, country: 'Ecuador' },
    { name: 'Michael',   email: 'michael@email.com',   age: 15, country: 'Colombia' },
    { name: 'Nicolás',   email: 'nicolas@email.com',    age: 43, country: 'Colombia' }
  ];

  $scope.OnClickSelect=function(item)
  {
   $scope.multipleDemo.push(item.name);
  }
  
  $scope.OnRemoveSelect = function(item) { 
   var index = $scope.people.indexOf(item.name);
   $scope.people.splice(index, 1); 
  }
  
  $scope.clearTag = function() {
    for(var i =0; i < $scope.multipleDemo.length; i++) {
      $scope.multipleDemo.splice($scope.multipleDemo[i], 1000);
      $scope.people.push($scope.multipleDemo[i]);
    }
  }

Angular-UI-Select 常见问题

ng-model 无法使用 $scope

上的简单变量

你不能写:

WRONG

<ui-select ng-model="multipleDemo"> <!-- Wrong -->
  [...]
</ui-select>

你需要写:

<ui-select ng-model="vm.multipleDemo"> <!-- Correct -->
  [...]
</ui-select>

有关详细信息,请参阅


更新

vm.multipleDemo doesn't work; I try $parent.multipleDemo - it works. I don't understand $parent. Why it works?

要使 vm.multipleDemo 正常工作,控制器必须初始化 vm 对象:

app.controller('DemoCtrl', function($scope, $http, $timeout) {
    $scope.vm = { multipleDemo: [] };

New AngularJS developers often do not realize that ng-repeat, ng-switch, ng-view, ng-include and ng-if all create new child scopes, so the [data hiding] problem often shows up when these directives are involved. (See this example for a quick illustration of the problem.)

This issue with primitives can be easily avoided by following the "best practice" of always have a '.' in your ng-models – watch 3 minutes worth. Misko demonstrates the primitive binding issue with ng-switch.

What are the nuances of scope prototypal / prototypical inheritance in AngularJS?

避免使用 $parent 来修复数据隐藏问题。这是一个脆弱的解决方案,因为在控制器和 ui-select 指令之间可以有不止一层的范围层次结构。我认为 $parent 的使用是 code smell,更深层次问题的症状。


更新#2

When I can use $ctrl in view and this in controller?

如果控制器是用"controller as"语法实例化的:

<body ng-controller="DemoCtrl as $ctrl">

    <ui-select ng-model="$ctrl.multipleDemo">
       <!-- -->
    </ui-select>

那就不用$scope:

app.controller('DemoCtrl', function($http) {
    this.multipleDemo =  [];

并且它避免了数据隐藏问题。

有关详细信息,请参阅

  • 'this' vs $scope in AngularJS controllers