javascript - 使用对话框更改 ng-repeat 循环内的值

标签 javascript angularjs

我想使用对话框然后使用函数来更改 ng-repeat 循环内项目的值。

例如,这是行不通的。

HTML

  <div ng-controller="TodoListController as todoList">
    <ul class="unstyled">
      <li ng-repeat="todo in todoList.todos">
        <label class="checkbox">
          <span>{{todo.name}}</span>
          <button ng-click="todoList.example(todo)">Click me</button>
        </label>
      </li>
    </ul>
    <div ng-if="todoList.show_box">
      <button ng-click="todoList.change_me()">Now change me</button>
    </div>
  </div>

JS

angular.module('todoApp', [])
  .controller('TodoListController', function() {
    this.todos = [{
      name: 'test 1'
    }, {
      name: 'test 2'
    }];

    this.example = function(v) {
      this.tmp = v;
      this.show_box = true;
    };

    this.change_me = function(v) {
       this.tmp.v.name = 'now it\'s ok';
    };
  });

完整示例

http://plnkr.co/edit/kobMJCsj4bvk02sveGdG

最佳答案

在函数内部,您的 this 将指向其他内容,而不是您的原始 Controller this,您可以通过执行以下操作来解决它。

var self = this;

self.todos = [{
  name: 'test 1'
}, {
  name: 'test 2'
}];

self.example = function(v) {
  self.tmp = v;
  self.show_box = true;
};

self.change_me = function(v) {
   self.tmp.v.name = 'now it\'s ok';
};

more info about this

关于javascript - 使用对话框更改 ng-repeat 循环内的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35650796/

相关文章:

javascript - 在首页加载时加载服务

javascript - 在鼠标悬停或调整浏览器大小之前不会加载内容

html - 我应该在哪个 HTML 元素上放置 ng-if?

javascript - Angular View 未加载

javascript - 如何将 fadeIn 添加到我的 show_hide() 函数中?

javascript - 如何更新数组中的对象

javascript - 使用 AngularJS 更改元素的类

javascript - http ://bsonspec. org/#/faq 的效果到底是什么?

javascript - HTML 中的交叉合并表列和行

javascript - 如何在表的一行中迭代时获取选择元素的 ng-model?