Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 创建角度服务_Angularjs_Node.js_Angular Services - Fatal编程技术网

Angularjs 创建角度服务

Angularjs 创建角度服务,angularjs,node.js,angular-services,Angularjs,Node.js,Angular Services,我想在Angular中创建一个服务来存储从Nodejs接收到的用户ID 因此,我创建了一个服务: MyApp.service('myservice', function(id) { var userId = id; return userId; }); 在我的控制器中: MyApp.controller("MyCtrl", function($scope, $http, myservice){ $scope.senduser = function(user){

我想在Angular中创建一个服务来存储从Nodejs接收到的用户ID

因此,我创建了一个服务:

MyApp.service('myservice', function(id) {
   var userId = id;
   return userId; 
});
在我的控制器中:

MyApp.controller("MyCtrl", function($scope, $http, myservice){

    $scope.senduser = function(user){

       $http.post("/login", user).then(function(response){
          if(response.data){

            console.log(response.data);
            myservice(response.data.id); //Store the data received from Node to myservice

            } else {
                console.log("No Data");
            }
        });
    }
});

我无法在服务中存储数据。如何将用户ID存储在服务中,以便以后能够访问它?

服务工厂函数将生成表示应用程序其余部分的服务的单个对象或函数

将您的服务代码更改为下面的代码

MyApp.service('myservice', function() {
    // setting some default value
       this.userId = 0;
});

MyApp.controller("MyCtrl", function($scope, $http, myservice){

    $scope.senduser = function(user){

    $http.post("/login", user).then(function(response){
      if(response.data){
        console.log(response.data);
        myservice(response.data.id); //Store the data received from Node to myservice
        myservice.userId = response.data.id

        } else {
            console.log("No Data");
         }
       });
    }
 });

服务代码可以如下所示:

 MyApp.factory('myservice', function() {
       var userId;

      setUserId = function(id) {
           userId = id;
       }

      getUserId = function() {
         return userId;
      }

    return {
    setUserId : setUserId,
    getUserId : getUserId
    }

    });
在控制器中,您可以设置并获得如下值:

MyApp.controller("MyCtrl", function($scope, $http, myservice){

$scope.senduser = function(user){

   $http.post("/login", user).then(function(response){
      if(response.data){

        console.log(response.data);
        myservice.setUserId(response.data.id); //Store the data received     from Node to myservice

        } else {
            console.log("No Data");
        }
    });
}
});

Im获取
ReferenceError:id未定义
this.userId=id请立即检查该值似乎没有添加到服务中。我从另一个控制器调用它,它仍然是0用相同的代码检查这个plunkr,设置并从服务中获取值,请提供您的完整版本,如果它仍然不起作用,您不应该在控制器中有$http。它属于需要此数据的服务。首先,它表示未定义
id
。我将id定义为0。如果我调用myservice.getUserId,则不会调用任何内容,它将提供我编辑过的
函数(){return userId;}
。现在检查一下。另外,当您调用getter时,请像调用myService.getUserId()一样调用它。
谢谢。现在工作。