I'm trying to wrap my head around the change with AngularJS regarding the components' lifecycle hooks, especially $onInit()
. I'm working with Todd Mottos Course, where he build a component which, in my opinion, should not work with 1.6, but it still does:
var repos = {
template: `
<div class="repos">
My Repos:
<ul>
<li ng-repeat="repo in $ctrl.list">
<a href="{{ repo.html_url }}">
{{ repo.name }}
</a>
({{ repo.stargazers_count }} stars)
</li>
</ul>
</div>
`,
controller: function (ReposService) {
var ctrl = this;
ctrl.list = [];
ReposService.getRepos().then(function (response) {
console.log(ctrl.list);
ctrl.list = response;
});
}
};
angular
.module('repos')
.component('repos', repos)
.config(function ($stateProvider) {
$stateProvider
.state('repos', {
url: '/repos',
component: 'repos'
});
});
I don't understand, why
controller: function (ReposService) {
var ctrl = this;
ctrl.list = [];
ReposService.getRepos().then(function (response) {
ctrl.list = response;
});
}
still works, I thought "ctrl.list" would have to be initialized inside an $onInit, otherwise it is still undefined?