AngularJS:$ scope没有定义?
我已经开始学习Angular JS,在AngularJS中我一直在为这个控制器代码获取'$ scope is not defined'控制台错误:任何想法?
服务:signup.js
    'use strict';
angular.module('crud')
  .service('Signup',function () {
         var data={
        email:$scope.email,password:$scope.password,confirmPassword:$scope.confirmPassword  
    }
    //console.log(data);
    $sails.post("/api/user",data)
      .success(function (data, status, headers, jwr) {
            $scope.users=data;
        //$scope.user=data;
      })
      .error(function (data, status, headers, jwr) {
       console.log(data);
       //console.log(headers);
        alert('Houston, we got a problem!');
      });       
  });
Signupcontroller.js
 'use strict';
    angular.module('crud')
      .controller('SignupCtrl', function ($scope,Signup) {
        // Using .success() and .error()
    }); 
介绍
看看以下答案:https://stackoverflow.com/a/22899880/1688441
  您不应该试图直接使用$scope从服务,因为它不可用。  控制器的$scope将包含一些变量/对象,然后您可以通过调用将其传递给您的服务。 
https://stackoverflow.com/a/22899880的答案显示了实现您希望执行的正确方法,并且几乎是您实际需要的结构(使用不同的名称)。
  很明显,您需要进行更改,例如重写他的save方法来执行HTTP POST,并通过对登录请求的响应联系服务器。  由于http请求是异步的,因此使用resource可能会更好。  请参阅:https://docs.angularjs.org/api/ngResource/service/$resource 
快速粗略示例
angular.module('crud').service('SignupService', ['$http', function ($http) {
    var path = '/api/user';
    var loginHttpPost = function (data) {
        return $http.post(path,data);   //Return the promise, or instead of a service make a resource, see: https://docs.angularjs.org/api/ngResource/service/$resource      
    };  
}])
angular.module('crud').controller('SignupCtrl', function ($scope,SignupService) {
        $scope.login = function () {
           var data={email:$scope.email,password:$scope.password,confirmPassword:$scope.confirmPassword}; 
           //HTTP calls are asynchronous. Maybe better to use resource. Must use promise.
           SignupService.loginHttpPost(data).success(function (data) {
                //Do whatever is needed
                $scope.loginResults = //assign result
           }).error(function (data) {
                //Do whatever is needed
                $scope.loginResults = //assign result
           });  
        };
    }
]);
  您没有正确注入$scope和服务Signup 。  $scope也仅在控制器中可用。  所以你需要将它传递给控制器。 
以下是正确的做法。
 'use strict';
  angular.module('crud')
  .controller('SignupCtrl',['$scope','Signup', function ($scope,Signup) {
  }]); 
                        链接地址: http://www.djcxy.com/p/77823.html
                        上一篇: AngularJS: $scope is not defined?
下一篇: AngularJS pass data from controller to another controller
