I have an AngularJS Service defined as follows:
function testService(testProvider) {
var ref = this;
ref.firstLevel = {};
ref.secondLevel = {};
initialize();
function initialize() {
testProvider.getData().then(function(result) {
ref.firstLevel = result;
ref.secondLevel.testData = result;
});
}
}
The testProvider
is a simple wrapper around $http.get that fetches data from a JSON. The controller copies over these properties:
function testController(testService) {
var vm = this;
vm.firstLevel = testService.firstLevel;
vm.secondLevel = testService.secondLevel;
}
When I create bindings in my template, the second level works, the first level doesn't.
<!-- Doesn't work -->
<p>{{vm.firstLevel.testProperty1}}</p>
<p>{{vm.firstLevel.testProperty2}}</p>
<!-- Does work -->
<p>{{vm.secondLevel.testData.testProperty1}}</p>
<p>{{vm.secondLevel.testData.testProperty2}}</p>
See this Plunker for a working example:
https://plnkr.co/edit/pLInqcaJNhhbQWbvTUEE
Why doesn't the first level example work?