letseasy
letseasy

Reputation: 47

AngularJS: $scope is not defined?

I've started learning Angular JS ,I keep getting '$scope is not defined' console errors for this controller code in AngularJS: any idea ?

Service :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()


    }); 

Upvotes: 0

Views: 5538

Answers (2)

Menelaos
Menelaos

Reputation: 26549

Intro

Have a look at the following answer: https://stackoverflow.com/a/22899880/1688441

You should not be trying to directly use $scope from a service since it isn't available. The $scope of your controller will contain some variables/objects that you could then pass to your service through calls.

The answer to https://stackoverflow.com/a/22899880 shows a correct way of implementing what you wish to do and is nearly the structure you essentially need (with different names).

Obviously you will need to make changes such as, rewriting his save method method to perform the HTTP post and contact the server with a response to the login request. Also since http requests are asynchronous it may be better to use a resource. See: https://docs.angularjs.org/api/ngResource/service/$resource

Quick Rough Example

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
           });  
        };
    }
]);

Upvotes: 4

Rahul Nikate
Rahul Nikate

Reputation: 6337

You are not correctly injecting $scope and the service Signup. Also $scope is only available in controller. So you need to pass it to service from controller.

Below is the right way of doing it.

 'use strict';

  angular.module('crud')
  .controller('SignupCtrl',['$scope','Signup', function ($scope,Signup) {


  }]); 

Upvotes: 0

Related Questions