简体   繁体   English

如何使用Angular 1.5中的组件为每个页面设置标题

[英]how to set header for each page using component in Angular 1.5

I started with Angular 1.5 component recently. 我最近开始使用Angular 1.5组件。 I am having various pages in my application. 我的申请表中有各种页面。 So I decided to create a <my-title> component which am using inside <my-header> component. 所以我决定创建一个在<my-header>组件中使用的<my-title> <my-header>组件。 As you see in navbar I have First, Second as navigation links. 正如您在导航栏中看到的,我有First,Second作为导航链接。 In my application there will be more parent child combinations. 在我的应用程序中,将有更多的父子组合。

I want to set title of each page by two way. 我想通过两种方式设置每个页面的标题。

  1. By giving it in partial <my-title>Home</my-title> (see 1.html or 2.html) (Manuel's answer satisfies this Scenario) 通过部分<my-title>Home</my-title> (参见1.html或2.html)(Manuel的答案满足此场景)
  2. Also I would like to set it from controller as well. 我也想从控制器设置它。 vm.title = "current page title" (Accepted Answer Satisfies This Scenario Only) vm.title = "current page title" (已接受的答案仅满足此方案)

I think any one thing can be done from above two options. 我认为任何一件事都可以从以上两个选项中完成。 There are two answers by different person for option 1(Deblaton) and option 2(Manuel). 不同的人对选项1(Deblaton)和选项2(Manuel)有两个答案。 Both answers satisfies respective scenarios. 两个答案都满足各自的情景。 I accepted who answered first correctly. 我接受了谁先正确回答。

Updated: If you see file "1.html" on plunker. 更新:如果您在plunker上看到文件“1.html”。 I am trying to set <my-title> First page</my-title> . 我正在尝试设置<my-title> First page</my-title> but that is not working. 但那不起作用。 My key idea is that developer will give <my-title>Current Page Title</my-title> on partial and it will be shown as per current page when he navigates across. 我的主要想法是开发人员将部分地给出<my-title>Current Page Title</my-title> ,并且当他导航时将按当前页面显示。

Please keep in mind I will be exposing only <my-title> to partial and controller. 请记住,我只会将<my-title>暴露给部分和控制器。 <my-header> will be at one place only. <my-header>仅限于一个地方。 Only Title will be changed. 只有标题会被更改。 If there are some new pages navigation links will be added to <my-header> . 如果有一些新页面,导航链接将添加到<my-header>

There is lot of code to copy-paste here. 这里有很多代码可以复制粘贴。 Please visit this PLUNKER . 请访问这个PLUNKER

module.component('myFirstApp', {
   templateUrl: "mainview.html",
   $routeConfig: [
     {path: '/', redirectTo: ['/First'] },
     {path: '/first', name: 'First', component: 'firstComponent'},
     {path: '/second', name: 'Second', component: 'secondComponent'}
   ]
 })

 module.component('firstComponent', {
   templateUrl: "1.html"
 });

 module.component('secondComponent', {
   templateUrl: "2.html"
 });

 module.component('myTitle', {
   template: '<h1>{{model.title}}</h1>'
 });

 module.component('myHeader', {
   templateUrl: "my-header.html"
 });

在此输入图像描述

To me, using component routing, it looks like a good idea to use a controller for handling your title. 对我来说,使用组件路由,使用控制器处理标题似乎是个好主意。 (with ui-router, I would have used the resolve option). (使用ui-router,我会使用resolve选项)。

I decided to inject $rootScope, and use it for sharing title value. 我决定注入$ rootScope,并用它来共享标题值。 You can also do it with a service. 您也可以使用服务。

so, the component : 所以,组件:

module.component('firstComponent', {
  templateUrl: "1.html",
  controller: ["$rootScope", function($rootScope){
    $rootScope.appVars = $rootScope.appVars || {};
    $rootScope.appVars.title = "Title from Page 1";
  }]
});

and the directive : 和指令:

module.component('myTitle', {
  template: '<h1>{{$root.appVars.title}}</h1>'
});

Here is your updated plnkr : https://plnkr.co/edit/6PCfznxsJzCPQtBm2oyN?p=preview 这是您更新的plnkr: https ://plnkr.co/edit/6PCfznxsJzCPQtBm2oyN p = preview

You are missing a couple of things in your code. 您在代码中遗漏了一些东西。

  1. You need to use bindings in order to pass parameters to components 您需要使用绑定才能将参数传递给组件
  2. You are using the word "model" in your templates to refer to model, but that's not the default one to do that. 您在模板中使用“模型”一词来指代模型,但这不是默认设置。 In here you can do 2 things, you can use $ctrl in templates or define controllerAs in component. 在这里你可以做两件事,你可以在模板中使用$ ctrl或在组件中定义controllerAs。

Component example: 组件示例:

module.component('myTitle', {
  template: '<h1>{{model.title}}</h1>',
  controllerAs: 'model',
  bindings: {
    title: '<'
  }
});

Use example: 使用示例:

<my-title title="'I am First'"></my-title>

Note the quotation marks inside the double quotation marks in "'I am First'". 请注意“'我是第一个'”中双引号内的引号。 You need both because you are passing a string as parameter. 你需要两个,因为你传递一个字符串作为参数。

In order to change the title in the header I created a service to allow the communication from components under ng-outlet and outer components because you can not pass data as bindings via routes. 为了更改标题中的标题,我创建了一个服务,允许来自ng-outlet和外部组件下的组件进行通信,因为您无法通过路由将数据作为绑定传递。

Your Plunker with the changes: https://plnkr.co/edit/ScpJYQItsMQlyd1Eacyi?p=preview 您的Plunker包含更改: https ://plnkr.co/edit/ScpJYQItsMQlyd1Eacyi?p = preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM