当我失去焦点时,不会触发ng-blur

时间:2016-03-27 00:21:23

标签: javascript angularjs

所以我一直试图通过双击来改变播放列表的标题。

我正在使用ng-blur来了解我何时失去对编辑的关注,但永远不会调用doneEditing函数。 (editTitle和done edit只是将playlist.editing设置为true和false以及console.log编辑和完成编辑)。

html的

<html>
  <head>
    <meta charset="UTF-8">
    <title>Playlist</title>

    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/style.css">    

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
  </head>

  <body>
    <article ng-app>
      <div class="todo-wrapper" ng-controller="TodoCtrl">
        <h2 ng-hide="playlist.editing" ng-dblclick="editTitle(playlist)">{{playlist.title}}</h2> 
        <input ng-show="playlist.editing" ng-model="playlist.title" ng-blur="doneEditing(playlist)" autofocus />
        <ul>
          <li ng-repeat="todo in todos">
            <input type="checkbox" ng-model="todo.done"/>
            <span class="done-{{todo.done}}">{{todo.text}}</span>
          </li>
        </ul>
        <form>
          <input class="add-input" placeholder="I need to..." type="text" ng-model="formTodoText" />
          <button class="add-btn" ng-click="addTodo()"><h2>Add</h2></button>
        </form>
        <button class="clear-btn" ng-click="clearCompleted()">Clear completed</button>
      </div>
    </article>
  <script src="js/index.js"></script>
  <script src='http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js'></script>
  </body>
</html>

.js

function TodoCtrl($scope) {

  $scope.todos = [
    {text:'Movie1', done:false},         
    {text:'Movie2', done:false}
  ];
  $scope.playlist = {title: "New Playlist", editing:false};

  $scope.getTotalTodos = function () {
    return $scope.todos.length;
  }; 

  $scope.addTodo = function () {
    $scope.todos.push({text:$scope.formTodoText, done:false});
    $scope.formTodoText = '';
  };

  $scope.clearCompleted = function () {
        $scope.todos = _.filter($scope.todos, function(todo){
            return !todo.done;
        });
  };

  $scope.editTitle = function (playlist) {
      playlist.editing = true;
  };

  $scope.doneEditing = function (playlist) {
      playlist.editing = false;
  };

}

我不确定我在这里做错了什么,因为我在小提琴(http://jsfiddle.net/davekr/F7K63/43/)上找到的例子在使用这些行时效果很好。

PS:我已经测试过在Firefox和Chrome上运行代码,从未调用过doneEditing函数。

编辑:上传完整的.html和.js文件

1 个答案:

答案 0 :(得分:0)

您需要将ng-model="playlist.title"添加到输入标记

<h2 ng-hide="playlist.editing" ng-click="editTitle(playlist)">{{playlist.title}}</h2> 
<input ng-show="playlist.editing" ng-model="playlist.title" ng-blur="doneEditing(playlist)" autofocus />

如果您将console.log("foo");添加到doneEditing功能,则可以看到它正常工作

修改

这是一个掠夺者 https://plnkr.co/edit/LoW9nkhYO5BIaOzuaxOX?p=preview

将AngularJS更新为最新版本或至少1.2&lt;