$scope.variable_name 是未定义的
$scope.variable_name is undefined
为了提高我在Angularjs方面的技能,我一直在使用特定框架的网站上工作。现在我收到此错误。
主控制器代码.js:
(function(){
angular.module('TimeWaste')
.controller('MainController', ['$scope', '$http', '$interval', function($scope, $http, $interval) {
if(localStorage['User-Data'] !== undefined)
{
$scope.user = JSON.parse(localStorage['User-Data']);
console.log($scope.user)
}
$scope.sendWaste = function(event) {
if(event.which === 13)
{
var request = {
user: $scope.user.username || $scope.user.email,
userId: $scope.user_id,
userImage: $scope.user.image,
content: $scope.newWaste
}
$http.post('api/waste/post, request').success(function(response){
console.log('fires')
}).error(function(error){
console.error(error);
})
}
};
}]);
}());
我通过控制台收到此错误:
Error: $scope.user is undefined
$scope.sendWaste@http://localhost:3000/app/main/main-controller.js:15:6
anonymous/fn@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js line 216 >
Function:2:319
Jc[b] .compile/</</e@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js:257:177
rf/this.$get</r.prototype.$eval@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js:133:442
rf/this.$get</r.prototype.$apply@https://ajax.googleapis.com/ajax/libs/angu larjs/1.4.9/angular.min.js:134:170
Jc[b] .compile/</<@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular. min.js:257:227
m.event.dispatch@https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js:4:8497
m.event.add/r.handle@https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js:4:5235
https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js
Line 107
据我所知,如果不定义localStorage['User-Data']
$user.scope
会undefined
。
但是我已经将localStorage['User-Data']
变量定义为
"{"email":"pavan.vasan@gmail.com","_id":"56c19d3a2c0b68545bd97793","username":"PLSV927","image":"/uploads/56c19d3a2c0b68545bd97793Fri Feb 19 2016 19:42:37 GMT-0800 (PST)morningbikeride.jpg"}"
有人可以告诉我出了什么问题吗? 是因为JSON.parse()
不工作还是我忽略了一些简单的错误?请帮帮我
编辑:我的坏人。我犯了一个错误,而不是在代码片段中$scope.user._id
,我把它放在$scope.user_id
.....我道歉
转换这个
$http.post('api/waste/post, request')
对此
$http.post('api/waste/post', request)
并在控制器开头将$scope.user
声明为对象
$scope.user={};
angular.module('TimeWaste') 应该是 angular.module('TimeWaste',[]) 如果这是你的第一个模块实例。
https://plnkr.co/edit/LKkAUN0U2DqPIhxY5ms9?p=preview
(function() {
angular.module('TimeWaste', [])
.controller('MainController', ['$scope', '$http', '$interval',
function($scope, $http, $interval) {
$scope.user;
if (localStorage['User-Data'] !== undefined) {
$scope.user = JSON.parse(localStorage['User-Data']);
console.log($scope.user)
}
$scope.sendWaste = function(event) {
if (event.which === 13) {
var request = {
user: $scope.user.username || $scope.user.email,
userId: $scope.user_id,
userImage: $scope.user.image,
content: $scope.newWaste
}
$http.post('api/waste/post', request).success(function(response) {
console.log('fires')
}).error(function(error) {
console.error(error);
})
}
};
}
]);
}());
相关文章:
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 如何检查管道中未定义的项目
- TypeError:无法读取属性'推'未定义的JavaScript
- $window.ga在AngularJS事件中未定义
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 无法获取属性'selectedIndex'的未定义引用或null引用
- 如何消除代码中的未定义和其他问题
- 骨干's Router.execute(callback,args,name)方法获取未定义的名称
- Sailsjs:TypeError:无法读取属性'name'的未定义
- 无法设置属性'name'的未定义JS
- 为什么 this.name 未定义
- 无法读取属性“”;name”;的未定义
- Javascript:“;无法读取属性“”;name”;未定义的“;本地javascript代码出错
- Internet Explorer中未定义constructor.name
- Javascript未定义为属性*name*
- 在数组中的复选框上调用.name将返回未定义
- 未捕获的错误:不匹配的匿名定义()模块:函数定义(name, global)
- 当使用window.frames[name]访问时,iframe contentWindow未定义