我第一次使用Angular Routes,最近我提出了一个关于刷新丢失一些数据的问题,我收到的答案是使用resolve。这似乎是我正在寻找的解决方案,但是我无法让它正常工作。
.config(['$routeProvider', function($routeProvider) {
//determines redirects go via shortcuts, clicking on the management icon on the main page sends the routeProvider /MG which it then uses to pull the relevant HTML file
$routeProvider
.when('/MG', {
controller: 'teammateController',
templateUrl: './assets/departments/department.html',
resolve: {
activeDepartment: function() {
return 'Management'
}
}
})
.when('/PO', {
controller: 'teammateController',
templateUrl: './assets/departments/department.html',
resolve: {
activeDepartment: function() {
return 'Programs Office'
}
}
})
.when('/SM', {
controller: 'teammateController',
templateUrl: './assets/departments/department.html',
resolve: {
activeDepartment: function() {
return 'Sales And Marketing'
}
}
})
.when('/', {
controller: 'teammateController',
templateUrl: 'home.html',
resolve: {
activeDepartment: function() {
console.log("working");
return 'home';
}
}
})
.otherwise({
controller: 'teammateController',
templateUrl: 'home.html',
resolve: {
activeDepartment: function() {
console.log("working");
return 'home';
}
}
})
}]);
app.controller('teammateController', function teammateController($scope, $http, $rootScope, $timeout, Idle, $location) {
$scope.deptName = activeDepartment();
});
我收到“ReferenceError:activeDepartment未定义”。控制台日志工作,我看到“工作”,所以我知道正在运行解决方案,但然后出现错误消息。我已经尝试了所有我能想到的东西而且我不能为我的生活看到我出错的地方。任何帮助将不胜感激。
答案 0 :(得分:2)
已解析的属性将注入您的控制器,请尝试以下操作:
app.controller('teammateController', function teammateController($scope, $http, $rootScope, $timeout, Idle, $location, activeDepartment) {
$scope.deptName = activeDepartment;
});
注意控制器末尾的附加参数。
答案 1 :(得分:1)
app.controller('teammateController', function teammateController($scope, $http, $rootScope, $timeout, Idle, $location) { $scope.deptName = activeDepartment(); });
应该是
app.controller('teammateController', function teammateController($scope, $http, $rootScope, $timeout, Idle, $location, activeDepartment) { $scope.deptName = activeDepartment; });
顺便说一下,如果你想缩小你的代码,你使用的语法就不会起作用了。