我正试图围绕 AngularJS 关于组件生命周期钩子的更改,尤其是$onInit()
. 我正在与 Todd Mottos Course 合作,他在那里构建了一个组件,在我看来,它不应该与 1.6 一起使用,但它仍然可以:
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'
});
});
不明白,为什么
controller: function (ReposService) {
var ctrl = this;
ctrl.list = [];
ReposService.getRepos().then(function (response) {
ctrl.list = response;
});
}
仍然有效,我认为“ctrl.list”必须在 $onInit 中初始化,否则它仍然是未定义的?