AngularJS
및 Firebase
을 사용하여 간단한 할일 메모 응용 프로그램을 채우고 있습니다. (AngularJS도 처음입니다!) To-do 노트는 $scope.todos
변수에 저장됩니다. 이것은 내 app.js
코드 :AngularJS - 배열 길이를 실시간으로 업데이트 할 수없는 이유는 무엇입니까?
myApp.controller('mainController', function($scope, $firebase) {
// connect to firebase
var ref = new Firebase("https://XXX.firebaseio.com/todos");
var fb = $firebase(ref);
$scope.todos = [];
// sync as object
var syncObject = fb.$asObject();
// 3-way data binding
syncObject.$bindTo($scope, "todos");
$scope.addNewTodo = function(){
var todoTitle = $(".add-todo").val();
var createdAt = new Date();
var element = {
title: todoTitle,
timestamp: createdAt,
done: false
};
fb.$push(element);
$(".add-todo").val("");
};
});
그리고 이것은 내 index.html
코드 :
<div class="container" ng-app="todoApp" ng-controller="mainController">
<div class="row">
<div class="col-md-6">
<div class="todolist not-done">
<h1>Todos</h1>
<input type="text" class="form-control add-todo" placeholder="Add todo" ng-enter="addNewTodo()">
<button id="checkAll" class="btn btn-success">Mark all as done</button>
<hr/>
<ul id="sortable" class="list-unstyled" ng-repeat="todo in todos">
<li class="ui-state-default">
<div class="checkbox">
<label>
<input type="checkbox" value=""/> {{ todo.title }}</label>
</div>
</li>
</ul>
<div class="todo-footer">
<strong><span class="count-todos">{{ todos.length }}</span></strong> Items Left
</div>
</div>
</div>
</div>
</div>
내가
$scope.todos
배열의 나머지 일하러 요소를 카운트
{{todos.length}}
을 추가했습니다. 그러나
$scope.todos
배열에서 요소를 추가하거나 제거 할 때 자동으로 업데이트되지 않습니다. 이것은 내가 조금 이상하다. 왜냐하면 나는 똑같은 일을한다면,
MeteorJS
이하는 것과는 아주 다르다.
저는 Firebase에 익숙하지 않지만 Firebase와 관련이있는 것으로 보입니다. 내 테스트에서 일하고있어 : http://plnkr.co/edit/TmGu2V92rz4BrompD8Dx?p=preview –