Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I access this ng-model variable in my controller?

Tags:

angularjs

http://jsfiddle.net/ntszE/

<input type="text" placeholder="0" ng-model="deposit" value="4" />&#8364; 
alert('begin test');
alert($scope.deposit);
alert('end test');

What am I doing wrong in binding the input value to a scope variable?

like image 416
Vincent Avatar asked Sep 12 '13 07:09

Vincent


1 Answers

You have to access the $scope in the controller. Check out the modification of your fiddle http://jsfiddle.net/lpiepiora/ntszE/2/

Basically you have to define a controller

function MyCtrl($scope) {
    $scope.deposit = 4;
    $scope.showValue = function() {
        alert($scope.deposit);
    };    
};

and then bind it using the ng-controller directive: ng-controller="MyCtrl".

like image 148
lpiepiora Avatar answered Sep 19 '22 09:09

lpiepiora