【问题标题】:Convey value from dynamic <select> to list将值从动态 <select> 传递到列表
【发布时间】:2017-10-16 06:40:14
【问题描述】:

所以在按钮点击我添加新的选择,在删除我删除数组的最后一个成员。现在我想将选定的值移动到下面的列表中。

这是我的代码:

  $scope.langInput = {
    count: 3,
    values: [1, 2],
    add: function() {
        this.values.push(this.count);
        this.count += 1;
        console.log(this.values);
    },
    remove: function() {
        this.values.pop();
        this.count -= 1;
        console.log(this.values);
    }
};

This是我的代码的工作演示。我想将选定的选项移动到&lt;ol&gt; 列表。提前致谢。

【问题讨论】:

  • 您是否尝试过使用listonchange 事件并将所选项目添加到ol
  • 不,我想不通你能编辑我的 plunker 并显示示例吗?
  • 我不知道如何在 Angular 中做到这一点,但这里是普通 JS (ES6) 中的示例。 jsfiddle.net/rpneLdma
  • 好吧,我在 angularJS 中需要这个,并且按钮必须存在。

标签: javascript angularjs angularjs-ng-repeat


【解决方案1】:

你可以把select写成:

<select ng-model="n.selected" ng-change="onChange(n.selected, $index)">

但是它要求langInput.values 应该是对象列表而不是整数

$scope.langInput = {
        count: 3,
        values: [
          {
            id:1,
            selected: "eng"  
          },
          {
            id:2,
            selected: "eng"
          }
          ],
        add: function() {
            this.values.push({id:this.count,selected: "eng"});
            this.count += 1;
            console.log(this.values);
        },
        remove: function() {
            this.values.pop();
            this.count -= 1;
            console.log(this.values);
        }
    };

    $scope.onChange = function(value, index){
      $scope.langInput.values[index].selected = value;
    }

Demo Plunker

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-01
    • 2016-02-17
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 2011-07-31
    相关资源
    最近更新 更多