#angularjs #angularjs-scope
#angularjs #angularjs-область
Вопрос:
У меня возникла проблема с добавлением пользовательской модели в приложение списка задач:
http://yeoman.io/codelab/write-app.html
Вот мой контроллер:
'use strict';
angular.module('test3App')
.controller('MainCtrl', function ($scope, localStorageService) {
var usersInStore = localStorageService.get('users');
var todosInStore = localStorageService.get('todos');
$scope.users = usersInStore amp;amp; usersInStore.split('n') || [];
$scope.todos = todosInStore amp;amp; todosInStore.split('n') || [];
$scope.$watch('users', function () {
localStorageService.add('users', $scope.users.join('n'));
}, true);
$scope.$watch('todos', function () {
localStorageService.add('todos', $scope.todos.join('n'));
}, true);
$scope.addTodo = function () {
$scope.todos.push($scope.todo);
$scope.todo = '';
};
$scope.removeTodo = function (index) {
$scope.todos.splice(index, 1);
};
$scope.addUser = function () {
$scope.users.push($scope.users);
$scope.users = '';
};
$scope.removeUser = function (index) {
$scope.users.splice(index, 1);
}; });
Я получаю сообщение об ошибке в этой строке:
localStorageService.add('users', $scope.users.join('n'));
TypeError: undefined is not a function
at Object.fn (http://localhost:9000/scripts/controllers/main.js:14:53)
at Scope.$digest (http://localhost:9000/bower_components/angular/angular.js:12251:29)
at Scope.$apply (http://localhost:9000/bower_components/angular/angular.js:12516:24)
at HTMLFormElement.<anonymous> (http://localhost:9000/bower_components/angular/angular.js:18626:21)
at HTMLFormElement.jQuery.event.dispatch (http://localhost:9000/bower_components/jquery/dist/jquery.js:4641:9)
at HTMLFormElement.elemData.handle (http://localhost:9000/bower_components/jquery/dist/jquery.js:4309:46)
Что я делаю не так?
Комментарии:
1. Как вы думаете
usersInStore amp;amp; usersInStore.split('n') || []
, что возвращает? Похоже, вы хотите большеusersInStore ? usersInStore.split('n') : []
2. Я бы поставил
console.log($scope.users)
перед вашим вызовомlocalStorageService.add('users', $scope.users.join('n'));
. Как отметил предыдущий комментатор, я предполагаю, что$scope.users
это пустое.3. Должно
$scope.users = '';
быть$scope.user = '';
?4. да, это была ошибка здесь …. спасибо, ребята!!