因此,我一直试图通过双击来更改播放列表的标题。
我正在使用ng-blur来知道何时失去对编辑的关注,但从未调用doneEditing函数。 (editTitle和完成编辑只是将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文件
最佳答案
您需要在输入标签中添加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 <
关于javascript - 失去焦点时不会触发ng-blur,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36242455/