Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

javascript - 如何基于AngularJS部分视图动态更改标头?(How to dynamically change header based on AngularJS partial view?)

I am using ng-view to include AngularJS partial views, and I want to update the page title and h1 header tags based on the included view.

(我正在使用ng-view包含AngularJS部分视图,并且我想根据所包含的视图更新页面标题和h1标头标签。)

These are out of scope of the partial view controllers though, and so I can't figure out how to bind them to data set in the controllers.

(但是,这些超出了部分视图控制器的范围,因此我无法弄清楚如何将它们绑定到控制器中的数据集。)

If it was ASP.NET MVC you could use @ViewBag to do this, but I don't know the equivalent in AngularJS.

(如果是ASP.NET MVC,则可以使用@ViewBag来执行此操作,但是我不知道AngularJS中的等效方法。)

I've searched about shared services, events etc but still can't get it working.

(我已经搜索了有关共享服务,事件等的信息,但仍然无法正常运行。)

Any way to modify my example so it works would be much appreciated.

(任何修改我的示例使其可行的方法将不胜感激。)

My HTML:

(我的HTML:)

<html data-ng-app="myModule">
<head>
<!-- include js files -->
<title><!-- should changed when ng-view changes --></title>
</head>
<body>
<h1><!-- should changed when ng-view changes --></h1>

<div data-ng-view></div>

</body>
</html>

My JavaScript:

(我的JavaScript:)

var myModule = angular.module('myModule', []);
myModule.config(['$routeProvider', function($routeProvider) {
    $routeProvider.
        when('/test1', {templateUrl: 'test1.html', controller: Test1Ctrl}).
        when('/test2', {templateUrl: 'test2.html', controller: Test2Ctrl}).
        otherwise({redirectTo: '/test1'});
}]);

function Test1Ctrl($scope, $http) { $scope.header = "Test 1"; 
                                  /* ^ how can I put this in title and h1 */ }
function Test2Ctrl($scope, $http) { $scope.header = "Test 2"; }
  ask by mikel translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I just discovered a nice way to set your page title if you're using routing:

(我刚刚发现了一种使用路由设置页面标题的好方法:)

JavaScript:

(JavaScript:)

var myApp = angular.module('myApp', ['ngResource'])

myApp.config(
    ['$routeProvider', function($routeProvider) {
        $routeProvider.when('/', {
            title: 'Home',
            templateUrl: '/Assets/Views/Home.html',
            controller: 'HomeController'
        });
        $routeProvider.when('/Product/:id', {
            title: 'Product',
            templateUrl: '/Assets/Views/Product.html',
            controller: 'ProductController'
        });
    }]);

myApp.run(['$rootScope', function($rootScope) {
    $rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
        $rootScope.title = current.$$route.title;
    });
}]);

HTML:

(HTML:)

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <title ng-bind="'myApp &mdash; ' + title">myApp</title>
...

Edit : using the ng-bind attribute instead of curlies {{}} so they don't show on load

(编辑 :使用ng-bind属性代替curlies {{}}这样它们就不会在加载时显示)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...