I'm trying to read a model from inside a controller. Say I have this controller
angular.module('app', [])
.controller('MainCtrl', function($scope) {
$scope.data = {title: 'foo'};
$scope.log = function() {
console.log($scope.data);
}
});
And this markup
<div ng-app="app" ng-controller="MainCtrl">
<input ng-model="data.title">
<button ng-click="log()">log</button>
</div>
When I press the button the first time it works, also if I modified the value, but all subsequent clicks just logs the first model again, no matter if the input data is changed.
What am I not getting? Should I use something else for this purpose? A service?
Fiddle: http://jsfiddle.net/J6jLz/1/
Update Curiously it works if I log the 'title' property prior to logging the actual data object: http://jsfiddle.net/Yf5kz/4/
Does this have to do with $apply on the scope maybe?
Try this instead
console.log(JSON.stringify($scope.data));