Rijo Rijo - 4 days ago 5
AngularJS Question

How to get Angularjs Hidden field value without any button action?

I want get the angularjs hidden field value without any button action. I'm new in angularjs if any link available for this solution please send to me

Code given below

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="TestController">
<input type="hidden" name="id" ng-model="id" ng-init="id=12"></input>
</div>
<script>
function TestController($scope) {

alert($scope.id);
}
</script>

Answer

Problem is, in controller alert is invoked before ng-init get set,

try this,

app.controller('myController', function($scope,$timeout) {
   $timeout(function() {
         alert($scope.id);
    })
});

DEMO

Comments