首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >angular js select ng-options

angular js select ng-options
EN

Stack Overflow用户
提问于 2014-05-23 14:44:20
回答 1查看 354关注 0票数 0

我有一个代码

代码语言:javascript
复制
app.controller('SelectCtrl', ['$scope', '$routeParams', '$http', '$q', 
    function($scope,    $routeParams, $http, $q) {

    var deferred = $q.defer();
    $http.get('json/newjson.json').success(function(responce) {
        deferred.resolve(responce);
    });

    deferred.promise.then(function(data) {
        $scope.values = data;
        $scope.recepients = [20, 23];
        $scope.ids = _.pluck($scope.values, "id");
        $scope.free = _.difference($scope.ids, $scope.recepients);
        recalc();
    });


    function recalc() {
        $scope.recepients.forEach(function(v, index) {
            $scope.vals[index] = [];
            $scope.ids.forEach(function(val, i) {
                if (_.indexOf($scope.free, val) > -1 || val === $scope.recepients[index]) {
                    $scope.vals[index].push($scope.values[i]);
                }
            });
        });
    }

    $scope.add = function() {
        if ($scope.recepients.length < $scope.values.length) {
            $scope.recepients.push($scope.free.shift());
            recalc();
        }
    };

    $scope.remove = function(index) {
        $scope.recepients.splice(index, 1);
        $scope.free = _.difference($scope.ids, $scope.recepients);
        recalc();
    }

    $scope.change = function() {
        $scope.free = _.difference($scope.ids, $scope.recepients);
        recalc();
    };

    $scope.vals = [];
}]);

和html视图

代码语言:javascript
复制
<table class="table">
  <tr data-ng-repeat="recepient in recepients">
    <td class="col-md-8">
        <select class="form-control" data-ng-model="recepients[$index]" data-ng-change="change()" data-ng-options="type.id as  type.name for (key , type) in vals[$index]"></select>
    </td>
    <td>
        <button class="btn btn-danger" data-ng-click="remove($index)"><span class="glyphicon glyphicon-remove"></span>remove</button>
    </td>
  </tr>
</table>
<button data-ng-click="add()" class="btn btn-success"><span class="glyphicon glyphicon-plus"></span>add</button>

如果我按下按钮来添加,就会在未使用的选项中添加一行。在chrome中它可以工作在chrome中,但是在IE9中如果我添加行并更改她,之前的选项也被更改了,这里是工作示例http://run.plnkr.co/plunks/43cXzghuFAFkbr2xW75e/#/

EN

回答 1

Stack Overflow用户

发布于 2014-05-23 15:16:47

我不会绑定到数组中的文字。

对象填充数组,可能只有一个value字段。{value: 3}我没有测试它,但是这应该可以解决你的问题。

更好的是,每个指令都有自己的作用域。在指令返回块中设置scope: true,也称为选项。

https://docs.angularjs.org/guide/directive

How to prevent that a scope is shared among directives n Angular?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23822640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档