A.K. Mahana
A.K. Mahana

Reputation: 1

angularjs how to access scope inside controller's behavior

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

Upvotes: 0

Views: 283

Answers (2)

Omri Aharon
Omri Aharon

Reputation: 17064

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

Upvotes: 0

Kamil
Kamil

Reputation: 1641

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/

Upvotes: 1

Related Questions