Skip to content

Instantly share code, notes, and snippets.

@SebSept
Created December 18, 2013 22:55
Show Gist options
  • Save SebSept/8ddcaad130ef9c091bd0 to your computer and use it in GitHub Desktop.
Save SebSept/8ddcaad130ef9c091bd0 to your computer and use it in GitHub Desktop.
why is json_var.val synced in service and not int_var ?
<html ng-app="myApp">
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
<script type="text/javascript">
var myApp = angular.module('myApp', []);
myApp.factory('myService', [
function() {
return {
json_var : {val:17},
int_var : 36
}
}
]);
myApp.controller('myCtrl', [ '$scope', 'myService',
function ($scope, myService)
{
$scope.json_var = myService.json_var; // doing this myService.json_var is updated when $scope.json_var changes
$scope.int_var = myService.int_var; // no sync for int
$scope.deb = function()
{
console.log($scope.int_var);
console.log( myService.int_var);
console.log($scope.json_var.val);
console.log( myService.json_var.val);
}
}
] );
</script>
</head>
<body>
<div ng-controller="myCtrl">
<input ng-model="json_var.val" />
<input ng-model="int_var" />
<button ng-click="deb()">DEB</button>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment