Angularjs Angular.js-作用域联接错误
将用户模型添加到待办事项列表应用程序时遇到问题: 这是我的控制器:Angularjs Angular.js-作用域联接错误,angularjs,angularjs-scope,Angularjs,Angularjs Scope,将用户模型添加到待办事项列表应用程序时遇到问题: 这是我的控制器: 'use strict'; angular.module('test3App') .controller('MainCtrl', function ($scope, localStorageService) { var usersInStore = localStorageService.get('users'); var todosInStore = localStorageService.get('todos');
'use strict';
angular.module('test3App')
.controller('MainCtrl', function ($scope, localStorageService) {
var usersInStore = localStorageService.get('users');
var todosInStore = localStorageService.get('todos');
$scope.users = usersInStore && usersInStore.split('\n') || [];
$scope.todos = todosInStore && 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)
localStorageService.add('users',$scope.users.join('\n'));
TypeError:undefined不是函数
at Object.fn(http://localhost:9000/scripts/controllers/main.js:14:53)
在范围内。$摘要(http://localhost:9000/bower_components/angular/angular.js:12251:29)
在范围内。$apply(http://localhost:9000/bower_components/angular/angular.js:12516:24)
在HTMLFormElement。(http://localhost:9000/bower_components/angular/angular.js:18626:21)
在HTMLFormElement.jQuery.event.dispatch(http://localhost:9000/bower_components/jquery/dist/jquery.js:4641:9)
位于HTMLFormElement.elemData.handle(http://localhost:9000/bower_components/jquery/dist/jquery.js:4309:46)
我做错了什么?您认为
usersInStore&&usersInStore.split('\n')| |[]
返回什么?看起来你想要更多的usersInStore?usersInStore.split('\n'):[]
我会在调用localStorageService.add('users',$scope.users.join('\n'))之前放置一个console.log($scope.users)
代码>。正如前面的评论所指出的,我猜测$scope.users
是空的代码>be$scope.user=''代码>?是的,这是这里的错误…谢谢大家!!