第一个ng-view模板的控制器没有加载

时间:2017-01-19 22:13:04

标签: javascript angularjs

我的目标网页组件的控制器在应用最初加载或从目标网页重新加载时不会检索任何数据。当我确认有数据时,我会一直得到undefined我的变量。

如果我点击#/index链接,数据加载正常,我的变量记录到控制台。知道为什么这只发生在网页的初始加载和我在登陆时重新加载的时候?

编辑:添加了我尝试与应用程序的主控制器一起登录的appUser值。基本上,我通过appStore运行服务来检索用户详细信息。然后我设置appUser值。我已确认appStore服务有效。

似乎MainLandingCtrlAppCtrl

之前运行

app.module.js:

angular.module('dup', ['ngRoute', 'mainLanding', 'unauthorized']);

app.value.js

angular.module('dup')
    .value('appUser', {

        fup: undefined
});

app.controller.js

angular.module('dup')
    .controller('AppCtrl', ['appStore', 'appUser', '$log',  function(appStore, appUser, $log) {

        var rcAppTemp = this;

        // User authorization
        appStore.authorizedUser()
            .then(function(response) {
                rcAppTemp.userDetails = response.data;
                appUser.fup = rcAppTemp.userDetails.fup;

            }, function(error) {
                $log.log('error');
            });

    }]);

app.config.js:

angular.module('dup')
.config(['$routeProvider',  function ($routeProvider) {
    $routeProvider
    // Main page
        .when('/', {
            template: '<main-landing></main-landing>'
        })
        .when('/unauthorized', {
            template: '<unauthorized></unauthorized>'
        })
        .otherwise({
            redirectTo: '/'
        });
}]);

主landing.controller.js:

angular.module('mainLanding')
    .controller('MainLandingCtrl', ['appUser', '$log', function(appUser, $log) {

        var mn = this;
        mn.fup = appUser.fup;

        $log.log(mn.fup);
}]);

主landing.component.js

angular.module('mainLanding')
.component('mainLanding', {
    bindings: {
    },
    templateUrl: 'components/main-landing/main-landing.template.html',
    controller: 'MainLandingCtrl'
});

的index.html:

<body ng-app="dup">
    <div ng-controller="AppCtrl as app">
        <!-- navigation bar (excluded for brevity) -->
    </div>
    <div ng-view></div>
</body>

主landing.template.html

<div class="row">
    <div class="small-12 medium-4 medium-offset-4 large-4 large-offset-4 columns">
        <div title="Home">
            <h5>Dup</h5>
            <ul>
                <li><a href="#/index"><strong>Index</strong></a></li>
                <li ng-show="$ctrl.fup><a href="#/unauthorized"><strong>Trouble</strong></a></li>
            </ul>
        </div>
    </div>
</div>

0 个答案:

没有答案