简体   繁体   English

角向组件附加DOM

[英]Angular attach DOM to Component

So in angularjs you had the possibility to define a directive and bind the html template to an already existing controller. 因此,在angularjs中,您可以定义指令并将html模板绑定到已经存在的控制器。 In principal this meant you could reuse the controller for multiple directives, therefore for multiple templates. 原则上,这意味着您可以将控制器重用于多个指令,因此可以用于多个模板。

angular
.module('App')
.component('Name', {
    templateUrl: 'some.html',
    controller: 'someController'
});

How can this be performed in Angular. 如何在Angular中执行此操作。 As far as I understood it in Angular Components are directives and always directly bind the html. 据我了解,它在Angular Components中是指令,并且总是直接绑定html。 Basically I want to use another view which only changes the html but keeps the same functionality. 基本上,我想使用另一个视图,该视图仅更改html,但保持相同的功能。

Edit: 编辑:

Basically I want this: 基本上我想要这样:

@Component({
    selector: 'jhi-exercise-list',
    templateUrl: '(MULTIPLE HTML TEMPLATE PATHS HERE)',
    providers: [                      
                ]
})
    export class className{

    //USING THE SAME COMPONENT CODE FOR THE MULTIPLE TEMPLATES

    constructor(){}
}

The only option I found so far would be through extending but I think thats overkill. 到目前为止,我发现的唯一选择是通过扩展,但是我认为那太过分了。

Define templateUrl as a function templateUrl定义为函数

You can specify templateUrl as a string representing the URL or as a function which takes two arguments tElement and tAttrs . 您可以指定templateUrl为代表的URL字符串或为这需要两个参数的函数 tElementtAttrs

The function takes the following arguments: 该函数采用以下参数:

  • tElement - template element - The element where the directive has been declared. tElement模板元素-声明了指令的元素。

  • tAttrs - template attributes - Normalized list of attributes declared on this element. tAttrs模板属性-在此元素上声明的属性的规范化列表。

For more information, see AngularJS Comprehensive Directive API Reference - templateURL 有关更多信息,请参见AngularJS综合指令API参考-templateURL

After more research it still seems that the best solution I could come up with is using inheritance. 经过更多研究,似乎我能想到的最佳解决方案是使用继承。

https://coryrylan.com/blog/angular-component-inheritance-and-template-swapping https://coryrylan.com/blog/angular-component-inheritance-and-template-swapping

He has a rather nice description of it 他对此有很好的描述

Thank you to all who helped me ;) 谢谢所有帮助我的人;)

There must be some directive in angularjs as ng-if or something like that. angularjs中必须有一些指令,如ng-if或类似的指令。

In Angular 4 you can do this as one of way as 在Angular 4中,您可以通过以下方式之一

<div *ngIf="x === 1"></div>
<div *ngIf="x === 2"></div>

And you can pass the value of x according to your need. 您可以根据需要传递x的值。

Added Solution 新增解决方案

import { Component, Input } from '@angular/core';
import { Blogger } from '../models/blogger';
import { BloggerProfileService } from '../service/blogger-profile.service';
import { SharedService } from '../service/shared.service';
import { AuthService } from '../auth-service.service';
import { Router } from '@angular/router';

@Component({
  selector: 'article-author-panel',
  templateUrl: './article-author-panel.component.html',
  styleUrls: ['./article-author-panel.component.css']
})
export class ArticleAuthorPanelComponent {

  @Input('templateType') templateType;
  author;
  blogger : Blogger;
  articleDt;
  user;

  @Input()
    get articleDate(){
      return this.articleDt;
    }

    set articleDate(date){
      this.articleDt = this.sharedService.getUTCDate(new Date(date));
    }
  @Input()

  set articleAuthor(author) {
    this.author = author;
    this.bloggerProfileService.getBlogger(author)
    .take(1)
    .subscribe((blogger) => {
      this.blogger = blogger;
    })
  }

  get articleAuthor() {
    return this.author;
  }

  constructor(
    private bloggerProfileService: BloggerProfileService,
    private sharedService: SharedService,
    private router : Router,
    private authService: AuthService
  ) {
    authService.user$
    .take(1)
    .subscribe((user) => {
      if(user) this.user = user;
    });
  }

  clickFollow(){
    if(this.user === null || this.user === undefined){
      this.router.navigate(['/']);
    }
  }

}

Template 模板

<ng-container *ngIf="templateType === 1">
    <div class="row no-gutters" style="border-top: 1px solid #a0a0a0; padding-top:5px;">
        <div class="col-2">
            <img src="http://www.publicdomainpictures.net/pictures/170000/velka/spinach-leaves-1461774375kTU.jpg" alt="Person" class="aap-image">
        </div>
        <div class="col-10">
            <div class="row">
                <div class="col-9">
                    <div class="aap-b-detail" *ngIf="blogger">
                        <span class="aap-b-name">{{ blogger.name }}</span>
                        <br />
                        <span class="aap-b-summary">{{ blogger.summary }}</span>
                    </div>
                </div>
                <div class="col-3">
                    <button class="aap-follow" (click)="clickFollow()">Follow</button>
                </div>
            </div>
        </div>
    </div>
</ng-container>
<ng-container *ngIf="templateType === 2">
    <div class="row no-gutters">
        <div class="col-2">
            <img src="http://www.publicdomainpictures.net/pictures/170000/velka/spinach-leaves-1461774375kTU.jpg" alt="Person" class="aap-image">
        </div>
        <div class="col-10">
            <div class="row">
                <div class="col-9">
                    <div class="aap-b-detail aap-b-detail-lh" *ngIf="blogger">
                        <span class="aap-b-name">{{ blogger.name }}</span>
                        <br />
                        <span class="aap-b-summary">{{ blogger.summary }}</span>
                        <br />
                        <span class="aap-b-date">{{ articleDate }}</span>
                    </div>
                </div>
                <div class="col-3">
                    <button class="aap-follow" (click)="clickFollow()">Follow</button>
                </div>
            </div>
        </div>
    </div>
</ng-container>

This is my component which I have created where according to the position I placed the component the layout changes. 这是我创建的组件,根据放置组件的位置布局会发生变化。 You can consider it as two different htmls connected together as normal if else keeping the behind code same. 您可以将其视为正常连接在一起的两个不同的html,前提是其他后面的代码保持不变。 According to my demand I did not use each variable. 根据我的需求,我没有使用每个变量。 Hope this helps. 希望这可以帮助。

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { LoadingComponent } from './loading.component'

@NgModule({
imports: [
CommonModule
],
declarations: [LoadingComponent],
exports: [LoadingComponent]
})
export class LoadingModule { }

IN this case am planning to reuse the loading component in multiple places. 在这种情况下,我计划在多个地方重用加载组件。 I will be injecting this module on specif area am intending to use loading component 我会在注射区域特异性上午该模块打算使用加载组件

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

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