AngularJS 1.6和$ onInit-Hook

时间:2018-08-10 07:36:57

标签: angularjs components ngoninit

我试图用AngularJS围绕组件的生命周期挂钩,尤其是+---------+------------------------+ |column_a |column_b | +---------+------------------------+ |[A, B, C]|[[A,aa], [B,bb], [C,cc]]| |[D, E] |[[D,dd], [E,ee]] | |[F] |[[F,ff]] | |[] |[] | +---------+------------------------+ 进行更改。我正在与Todd Mottos Course合作,他在其中构建了一个组件,我认为该组件不应与1.6一起使用,但仍然可以:

$onInit()

我不明白,为什么

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'
        });
    });

仍然有效,我认为“ ctrl.list”必须在$ onInit内部进行初始化,否则仍未定义?

0 个答案:

没有答案