$scope.variable_name 是未定义的

$scope.variable_name is undefined

本文关键字:未定义 name scope variable      更新时间:2023-09-26

为了提高我在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.scopeundefined

但是我已经将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);
            })
          }
        };
      }
    ]);
}());