标签: angular2-template

如何预加载angular2视图,这样第一次加载时不会闪烁白页?

我正在用简单的JS构建一个有角度的2应用程序.我的问题是,当我从一个页面更改为另一个页面时,它会闪烁一个白页,直到呈现新视图.仅在我第一次访问它们时才会发生这种情况.如果我第二次进入相同路线,则页面加载时没有白页.我捕获屏幕以更好地解释我的问题:https://drive.google.com/file/d/0B5pOsXT-4rdTbmtuVnlUdXRMdE0/view?usp =sharing

这是我的主要观点:

<router-outlet></router-outlet>

<nav id="menu">
    <a [routerLink]="['Page1']">Page 1</a>
    <a [routerLink]="['Page2']">Page 2</a>
    <a [routerLink]="['Page3']">Page 3</a>
    <a [routerLink]="['Page4']">Page 4</a>
    <a [routerLink]="['Page5']">Page 5</a>
</nav>
Run Code Online (Sandbox Code Playgroud)

RouteConfig的定义如下:

AppComponent = ng.router
    .RouteConfig([
        {path: '/page1', name:"Page1", component: Page1Component, useAsDefault: true },
        {path: '/page2', name:"Page2", component: Page2Component},
        ...
    ])
    (AppComponent);
Run Code Online (Sandbox Code Playgroud)

我的所有组件都有.html文件和.css文件,如下所示:

Page1Component = ng.core
    .Component({
        selector: 'rl-page1',
        templateUrl: 'app/components/page1/view.html',
        styleUrls: ['app/components/page1/style.css'],
        directives: [ng.router.ROUTER_DIRECTIVES]
    })
    .Class({
        constructor: [ng.router.Router, function(router) {
            this.router = router;


        }]
    });
Run Code Online (Sandbox Code Playgroud)

从我访问它们后看起来像角度缓存页面,在第二次访问时将立即显示(没有白色闪烁).我搜索我如何能够解除这个问题,但我找不到任何答案或方法.

如果您需要更多代码,请与我们联系.

(我不在实际应用中使用Page1,Page2,仅针对此问题,我更改为更清晰例如)

谢谢.

编辑:2016-03-18

我尝试使用DynamicComponentLoader加载隐藏div中的所有组件.代码在AppComponent中如下所示:

ngOnInit: function(){
    this.dcl.loadIntoLocation(Page1Component, this.elementRef, …
Run Code Online (Sandbox Code Playgroud)

angular2-template angular2-routing angular

6
推荐指数
1
解决办法
5393
查看次数

使用数字管道抛出InvalidPipeArgumentException(管道'DecimalPipe'的参数'1'无效)

我想写一些数字<input>并{{}}通过管道将其动态显示为十进制内部.它会引发异常.这是我的代码:

app.template.html:

<h1>amount = {{amount|number:'1.2-2'}}</h1>
<input [(ngModel)]="amount"/>
Run Code Online (Sandbox Code Playgroud)

app.component.ts:

import {Component} from '@angular/core';
@Component({
  selector: 'my-app',
  templateUrl: 'app/app.template.html'
})
export class AppComponent {
  private amount:number;
}
Run Code Online (Sandbox Code Playgroud)

Plunker:http://plnkr.co/edit/I7ynnwBX4DWJfHNPIPRu?p =preview

将任何数字写入输入并查看控制台中抛出的异常.


编辑:

根据rinukkusu建议的工作代码:

app.template.html:

<h1>amount = {{amount|number:'1.2-2'}}</h1>
<input [ngModel]="amount" (ngModelChange)="onChange($event)"/>
Run Code Online (Sandbox Code Playgroud)

app.component.ts:

import { Component } from '@angular/core';
@Component({
  selector: 'my-app',
  templateUrl: 'app/app.template.html'
})
export class AppComponent {
  private amount:number;

  onChange($event) {
    this.amount = +$event;
  }
}
Run Code Online (Sandbox Code Playgroud)

这+旁边$event是非常重要的,是什么让从字符串转换到一些可能的!

typescript angular2-template angular2-pipe angular

6
推荐指数
1
解决办法
8909
查看次数

Angular 2 - 用于创建和编辑的单个表单组件

我正在寻找一些好的建议/最佳实践来重用我的表单组件.

数据模型:

class Contact {
  id?: String;
  name: String;
}
Run Code Online (Sandbox Code Playgroud)
  • 当创建一个新Contact的时,当然没有id,这就是为什么它在模型中是可选的.

  • 当编辑一个Contact存在id,但它是不可编辑,因此它不是形式的一部分.

行为:

"编辑"和"创建"视图应使用相同的表单@Component,因为可用字段相同,验证约束也相同.

但两种观点必须有不同的行动.例如,"编辑"视图必须具有"删除并重新加载"按钮,当然两个视图的"保存"按钮的行为必须不同("创建"发出POST请求,"编辑发出PATCH请求").

我尝试了什么/问题:

我创建了一个ContactCreateComponent和一个ContactEditComponent,它们都<contactForm [contact]="contact"></contactForm>在他们的模板中.因为两个视图必须有不同的按钮和动作,所以我确实将Buttons放在Create和Edit Components中.

在ContactFormComponent有<form [formGroup]="form">和<input formControlName="name">标签.

现在我无法弄清楚如何从ContactFormComponent单击"保存"按钮时拉出表单数据.

思想/想法:

我可以FormGroup在Create和Edit Components中定义,然后通过FormGroup实例将实例传递给Form Component @Input.这样我就可以读取/更新/重置表单数据.但后来我不得不FormGroup两次编写整个定义和验证器,但在我看来,这应该保留在Form Component中.

(我不想在@Input属性上使用双向绑定,因为它Contact是不可变的)

你知道如何解决这个问题吗?

angular2-forms angular2-template angular

6
推荐指数
1
解决办法
4625
查看次数

无法将脚本文件添加到组件html

我在index.html(root)中有一个脚本文件引用; index.html的:

    <script src="page1.js"></script>
<script src="assets/sliderfunctions.js"></script>
<script src="assets/js/custom.js"></script>
Run Code Online (Sandbox Code Playgroud)

这里不需要sliderfunctions.js,它包含一些关于滑块的特定功能,所以我把它带到slider.component.html文件,但是你猜它不能正常工作,因为我实际上从未加载到浏览器..

slider.component.ts:

import {Component,ViewEncapsulation} from '@angular/core';

@Component({
    selector:'app-slider',
    templateUrl:'../templates/slider.component.html'
})
export class SliderComponent{
    constructor(){}
}
Run Code Online (Sandbox Code Playgroud)

slider.component.html:

 <script src="page1.js"></script>
<script src="assets/sliderfunctions.js"></script>
<script src="assets/js/custom.js"></script>
Run Code Online (Sandbox Code Playgroud)

(我带了3个,因为脚本的顺序很重要)所以我想得到一个特定的点,我已经在网上搜索但是无法弄清楚如何在组件模板中添加脚本文件

typescript angular2-template angular

6
推荐指数
1
解决办法
1万
查看次数

预填充输入在`FormGroup`中的输入 - Angular2

我正在使用Angular2 - Reactive Forms.一切正常,直到我想在Form中的一个字段中显示预先填充的值.

场景:页面上有多个按钮,每个按钮打开一个带有字段的表单

  1. 名称
  2. 电子邮件
  3. 信息
  4. 产品代码 - >此值应根据服务项目代码预先填充.

失败的场景:产品代码输入值变为空.

TS代码:

import { FormGroup, FormBuilder, Validators } from '@angular/forms';
queryForm: FormGroup;
constructor(private _productService: ProductService, fb: FormBuilder) {
    this.queryForm = fb.group({
        'name': [null, Validators.compose([Validators.required, Validators.minLength(5)])],
        'email': [
            null, [Validators.required, Validators.email]
        ],
        'message': [null,Validators.compose([Validators.required, Validators.minLength(5)])],
        'pcode': [
            null
        ],
    })
}
Run Code Online (Sandbox Code Playgroud)

HTML表格:

<div *ngFor="let item of product">
<form action="#" [formGroup]="queryForm" 
 (ngSubmit)="submitForm(queryForm.value)" method="post" 
  novalidate="" class="text-left note" id="f_{{item.productId}}">
    [ .... rest of the fields ...]
    <div class="form-group hidden">
          <input type="hidden " …
Run Code Online (Sandbox Code Playgroud)

angular2-forms angular2-template angular

6
推荐指数
1
解决办法
8451
查看次数

如何为枚举Angular 4打字稿显示不同的字符串

我有一个基于字符串的枚举.在不更改枚举类的情况下,我需要在模态上显示不同的字符串.我的obj包含枚举.我的代码如下:

ENUM:

  export enum FooEnum {
        ONE,
        TWO,
        THREE
    }
Run Code Online (Sandbox Code Playgroud)

HTML:

<select class="form-control"
                    type="text"
                    id="foo"
                    [(ngModel)]="obj.foo"
                    required
                    name="foo"
                    #foo="ngModel">
              <option *ngFor="let foo of fooToList()" [ngValue]="foo">{{foo}}</option>
            </select>
Run Code Online (Sandbox Code Playgroud)

打字稿:

  fooToList(): Array<string> {
    const keys = Object.keys(FooEnum);
    return keys.slice(keys.length / 2);
  }
Run Code Online (Sandbox Code Playgroud)

我想将ONE视为Un,TWO视为Deux等.你能帮我一下吗?

注意:我还包括角度2,因为2和4非常相似.

enums typescript angular2-template angular

6
推荐指数
1
解决办法
2万
查看次数

有没有办法使用Angular 4+模板来确保所有ng-template ID都是唯一的?

Angular 4+提供了一种在模板文件中执行if/else逻辑的方法,例如

<ng-container *ngIf="isSaveSuccess; else elseBlock">
  Save succeeded.
</ng-container>
<ng-template #elseBlock>
  Save failed.
</ng-template>
Run Code Online (Sandbox Code Playgroud)

但是,这需要您指定唯一的ng-template ID.我们对使用这种方法意外引入重复的"elseBlock"模板感到担心,例如

<ng-container *ngIf="isSaveSuccess; else elseBlock">
  Save succeeded.
</ng-container>
<ng-template #elseBlock>
  Save failed.
</ng-template>
...
<ng-container *ngIf="isUpdateSuccess; else elseBlock">
  Update succeeded.
</ng-container>
<ng-template #elseBlock> <!-- duplicate id -->
  Update failed.
</ng-template>
Run Code Online (Sandbox Code Playgroud)

有没有办法lint Angular模板文件来检测重复的ng-template ID?

lint tslint angular2-template angular codelyzer

6
推荐指数
0
解决办法
417
查看次数

Angular 2 Observable with ChangeDetectionStrategy.OnPush 和异步管道不起作用

我使用 observable 将列表从服务返回到我的组件,在我的组件中我使用 ChangeDetectionStrategy.OnPush 并且在模板中我使用异步管道,希望这会带来一些性能优势,因为更改检测不是一直执行但仅当有新内容可用时。

以下是我的服务:

import { Injectable, Inject, EventEmitter } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';

@Injectable()
export class ListService {

    public _list$: Subject<any[]>;

    private list: any[] = [];

    constructor() {
        this._list$ = <Subject<any>>new Subject();
    }

    get list$() {
        return this._list$.asObservable();
    }

    loadList() {
        //if this.list is populated through an http call the view is updated, 
        //if its a static list like below, it doesn't trigger view update.
        //this.list = …
Run Code Online (Sandbox Code Playgroud)

observable angular2-template angular2-services ngrx angular

5
推荐指数
1
解决办法
1744
查看次数

Angular2 输入组件在焦点上添加类

在此处输入图片说明我正在尝试使用 Anuglar 2 创建一个输入文本字段,它在 foucs 上应该如下图所示:在此处输入图片说明

我有一个 .tn_blue 类,当元素处于焦点时,我想将其添加到 div(下面以粗体显示)。

我无法在组件上使用主机绑定焦点:

我的 ts 代码:

import {Component, ElementRef, Renderer, Input, OnInit} from '@angular/core';


@Component({
  selector    : 'my-textfield',
  templateUrl : 'path-to/textfield.component.html',
  styleUrls   : ['path-to/textfield.component.css'],
  host: {
      '(focus)':'_setInputFocus(true)',
      }
})

export class Textfield implements OnInit{

  @Input() iconBoxTextfieldConfig:any;

  inputFocusClass: boolean;

      _setInputFocus(isFocus:boolean) {
        this.inputFocusClass = isFocus;
        console.log("he he he ")
      }


  elementRef: ElementRef;

  constructor(private el: ElementRef,  public renderer: Renderer) {
    this.elementRef = el;
  }

  ngOnInit(){

      this.inputFocusClass = true;
  }


}
Run Code Online (Sandbox Code Playgroud)

HTML代码:

<div class="tn-formfield" *ngIf="iconBoxTextfieldConfig">
  <div …
Run Code Online (Sandbox Code Playgroud)

angular2-directives angular2-template angular

5
推荐指数
2
解决办法
1万
查看次数

Angular2:渲染/重新加载组件的模板

理想情况下,我需要重新加载/重新呈现组件的模板,但如果有更好的方法,我会很乐意实现它.

期望的行为:

所以,我有一个菜单元素的组件.当(在另一个组件中)我单击一个IBO (某种'客户',每个说)被点击时,我需要添加(我正在使用*ngIf)菜单中的新选项,即IBO详细信息和子列表.

IBOsNavigationElement 组件(菜单组件):

@Component({
    selector: '[ibos-navigation-element]',
    template: `
        <a href="#"><i class="fa fa-lg fa-fw fa-group"></i> <span
                class="menu-item-parent">{{'IBOs' | i18n}}</span>
        </a>
        <ul>
            <li routerLinkActive="active">
                <a routerLink="/ibos/IBOsMain">{{'IBOs Main' | i18n}} {{id}}</a>
            </li>
            <li *ngIf="navigationList?.length > 0">
                <a href="#">{{'IBO Details' | i18n}}</a>
                <ul>
                    <li routerLinkActive="active" *ngFor="let navigatedIBO of navigationList">
                        <a href="#/ibos/IboDetails/{{navigatedIBO['id']}}">{{navigatedIBO['name']}}</a>
                    </li>
                </ul>
            </li>
        </ul>
    `
})
export class IBOsNavigationElement implements OnInit {
    private navigationList = <any>[];


    constructor(private navigationService: NavigationElementsService) {
        this.navigationService.updateIBOsNavigation$.subscribe((navigationData) …
Run Code Online (Sandbox Code Playgroud)

typescript angular2-template angular

5
推荐指数
1
解决办法
1万
查看次数