0

I am totally new on AngularJS and I try to figure out what's wrong with my code.

In general it looks that is a variable scope issue but I don't know how to solve it:

here is my code:

'use strict';

(
    function() {
        /**
         * @name angularApp.controller:LoginpageCtrl
         * @description This controller is responsible to manipulate the user login process.
         * @param themeConfigs Contains the site themeConfiguration options.
         */
        var $userRegisterCtrl = function ( $window, $timeout, user, themeConfigs ) {
            var $this = this;
            $this.theme_configs = themeConfigs;
            $this.username = '';
            $this.email = '';
            $this.password = '';
            $this.error = '';
            $this.success = '';

            $this.register = function() {
                user
                    .register(
                        $this.username,
                        $this.password,
                        $this.email
                    )
                    .then(
                        function( ) {
                            $this.success = 'Your account has been successfully created.<br />';
                            $this.success += 'Soon you will redirected in login page.';

                            $timeout(
                                function() {
                                    $window.location.href = '/#/login';
                                },
                                4000
                            );
                        },
                        function( $error_message ) {
                            console.log( $error_message );
                        }
                    );
            };
        };

        $userRegisterCtrl.$inject = [ '$window', '$timeout', 'user', 'themeConfigs' ];

        angular.module('angularApp' ).controller( 'userRegisterCtrl', $userRegisterCtrl );
    }
)();

and the error I get is the following:

TypeError: Cannot set property 'success' of undefined
    at register.js:27
    at processQueue (angular.js:14745)
    at angular.js:14761
    at Scope.parent.$get.Scope.$eval (angular.js:15989)
    at Scope.parent.$get.Scope.$digest (angular.js:15800)
    at Scope.parent.$get.Scope.$apply (angular.js:16097)
    at done (angular.js:10546)
    at completeRequest (angular.js:10744)
    at XMLHttpRequest.requestLoaded (angular.js:10685)

So the line 27 related with the following two lines:

$this.success = 'Your account has been successfully created.<br />';
$this.success += 'Soon you will redirected in login page.';

but why the $this.success is outside scope ? And how can I solve that issue ? Is there any trick related to promisses?

6
  • Why you use this in AngularJS? Why you don't use scope? Commented Nov 14, 2015 at 11:25
  • Also, why you name your variables with $ at start? Usually, angularJS objects should be named this way Commented Nov 14, 2015 at 11:26
  • I use the controllers with controllerAs that's why I use the this. I have assign the this into $this because my IDE marked the this as out of scome in the promisse callback. I also try the this but doesn't work. :( Commented Nov 14, 2015 at 11:26
  • 1
    can you try renaming $this to _this? usually angular uses $ for its services and variables, and your problem has nothing to do with the scope variable but with $this not being defined Commented Nov 14, 2015 at 11:31
  • that was the problem !!! I just chang it and it worked :) Thanks ;) If you like place your comment as answer to upvote you :) Commented Nov 14, 2015 at 11:33

1 Answer 1

1

can you try renaming $this to _this? usually angular uses $ for its services and variables,

  • $index
  • $id
  • $this

all examples

Your problem has nothing to do with the scope variable but with $this not being defined.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.