我试图从链接的另一个 JS 文件中编写的不同 Controller 获取变量。我想显示从不同 Controller 分配的页面标题。如果我路由到 extract.html,我必须在 html 标题中显示“Extract Home”文本,我在 extract.js 中声明了此变量
请帮助我,提前致谢
Main.html
<html lang="en" ng-app="routing" ng-controller="landingCtrl">
<title>{{page_location_text}} - Noble Institute</title>
<body>
<div ng-view></div>
</body>
</html>
<script>
var routingApp = angular.module("routing", ["ngRoute","extract"]);
routingApp.controller("landingCtrl",function($scope){
$scope.page_location_text = $scope.page_location;
});
routingApp.config(function($routeProvider) {
$routeProvider
.when("/", {
templateUrl : "index.html"
})
.when("/extract", {
templateUrl : "extract.html",
controller : "extractCtrl"
});
});
</script>
Extract.js for Extract.html
<script>
var app = angular.module('extract', ['angularUtils.directives.dirPagination',"ngSanitize", "ngCsv"]);
app.controller('extractCtrl',function($scope, $http, $routeParams, filterFilter){
$scope.page_location = "Extract Home";
});
</script>
最佳答案
您可以使用路线设置标题并将标题放在 html 上,例如:
<title ng-bind="title">myApp</title>
并在路由配置中设置标题,如下所示:
.when("/extract", {
title : 'Extract Home',
templateUrl : "extract.html",
controller : "extractCtrl"
});
并为您的 Angular 应用程序设置运行方法:
app.run(['$rootScope', '$route', function($rootScope, $route) {
$rootScope.$on('$routeChangeSuccess', function() {
document.title = $route.current.title;
});
}]);
关于javascript - 访问在angularjs中不同 Controller 中声明的路由页面 Controller 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40044159/