gpt4 book ai didi

javascript - $scope.variable_name 未定义

转载 作者:行者123 更新时间:2023-12-03 07:38:14 26 4
gpt4 key购买 nike

为了提高我在 angularjs 方面的技能,我一直在使用特定框架开发网站。现在我收到此错误。

main-controller.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

据我所知,$user.scopeundefined如果localStorage['User-Data']未定义。

但我已经有了 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)

并在 Controller 的开头声明$scope.user为对象

$scope.user={};

关于javascript - $scope.variable_name 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35519678/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com