我的目标网页组件的控制器在应用最初加载或从目标网页重新加载时不会检索任何数据。当我确认有数据时,我会一直得到undefined
我的变量。
如果我点击#/index
链接,数据加载正常,我的变量记录到控制台。知道为什么这只发生在网页的初始加载和我在登陆时重新加载的时候?
编辑:添加了我尝试与应用程序的主控制器一起登录的appUser
值。基本上,我通过appStore
运行服务来检索用户详细信息。然后我设置appUser
值。我已确认appStore
服务有效。
似乎MainLandingCtrl
在AppCtrl
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>