我的home.html页面中有一个控制器作为HomeController,我想在about.html页面中调用相同的控制器及其内容。以下代码用于主页,它将显示用户的名字和姓氏,我想调用相同的功能,如在about us页面中调用名字和姓氏。怎么做?截至目前,我刚刚复制了整个并粘贴在about控制器中,只是将控制器名称从HomeController更改为AboutController。
(function(){ '使用严格的';
angular
.module('app')
.controller('HomeController', HomeController);
HomeController.$inject = ['UserService', '$rootScope','$scope', '$http'];
function HomeController(UserService, $rootScope,$scope, $http) {
var vm = this;
vm.user = null;
vm.allUsers = [];
vm.deleteUser = deleteUser;
initController();
function initController() {
loadCurrentUser();
loadAllUsers();
}
function loadCurrentUser() {
UserService.GetByUsername($rootScope.globals.currentUser.username)
.then(function (user) {
vm.user = user;
});
}
function loadAllUsers() {
UserService.GetAll()
.then(function (users) {
vm.allUsers = users;
});
}
function deleteUser(id) {
UserService.Delete(id)
.then(function () {
loadAllUsers();
});
}
$http.get('webservice').
then(function(response) {
$scope.user_message = response.data.data;
});
}
})();