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?
thisin AngularJS? Why you don't use scope?$at start? Usually, angularJS objects should be named this waycontrollerAsthat's why I use thethis. I have assign thethisinto$thisbecause myIDEmarked thethisas out of scome in the promisse callback. I also try thethisbut doesn't work. :(