0

i have a problem while accessing $scope inside controller's behavior. code is like below.

<body id="main_body" ng-controller="FormController as frmCtrl">
<form id="form_991905" class="appnitro" name="loginForm" ng-submit="loginForm.$valid && frmCtrl.doLogin()" novalidate>
    <div class="form_description">
        <h2>Login Form</h2>     
    </div>
    <ul>                
        <li id="li_1" >
            <label class="description" for="username">Username </label>
            <div>
                <input name="username" class="element text medium crequired email" type="email" ng-model="login.username" form-validator />
                <div class="errBx"></div>
            </div>
        </li>       
        <li id="li_2" >
        <label class="description" for="password">Password </label>
            <div>
                <input name="password" class="element text medium crequired" type="text" ng-model="login.password" form-validator/>
                <div class="errBx"></div>
            </div>
        </li>               
        <li class="buttons">            
            <input id="saveForm" class="button_text" type="submit" name="submit" value="Submit"/>
        </li>
    </ul>
</form>

i want to access the $scope.login.username inside the method

controller code..... 

this.login = function(){
   console.log($scope.login.username);
}

// controller code
3
  • What problem you have? Commented Apr 7, 2015 at 8:37
  • Also when you are using controllerAs frmCtrl, you must prefix the ng-model with that. So it must be frmCtrl.login.username , Inside your controller it must be this.login.username Commented Apr 7, 2015 at 8:38
  • as i mentioned i want to access $scope variable inside that controller's method. is it possible ? Commented Apr 8, 2015 at 14:14

2 Answers 2

1

You are using "controllerAs" syntax, so there is no $scope available. In the view you can access it via frmCtrl.login.username or in the contoller you can try this.login.username.

You can learn more about it here: http://www.johnpapa.net/angularjss-controller-as-and-the-vm-variable/

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

2 Comments

i went through the article, but i need to update $scope.xyz rather the ng-model value. is it possible?
There is no $scope value when using constrollerAs syntax... You can assign any property to this and use it anywhere you want.
0

You're using ctrl as syntax, therefore your ng-model show be as follows:

ng-model="frmCtrl.login.password"

And function should change to:

this.login = function(){
   console.log(this.login.username);
}

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.