标签: angular2-template

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万
查看次数

如何在浏览器中缓存 svg(嵌入为 &lt;object&gt;)

我有一个由多个组件组成的 angular2 应用程序,它在其模板中嵌入了(参数)不同的小 svg 文件。这个选择的原因是我需要与 svg 进行交互并操作 svg 文件的内部 DOM。另外我想引用一个 svg 文件,而不是在模板中的多个位置使用内联 svg,它在组件模板中更干净。

我的问题是这些 svg 文件没有被缓存在浏览器中,并且在加载包含多个组件(呈现这些 svg 文件)的页面时,我注意到在页面完成加载之前有一些小延迟。这远非最佳,如果我不能解决这个问题,我会考虑回到内联 svg。

我的目标是能够让组件模板指向一个 svg 文件,并且当这个组件第一次加载时,它不会从服务器发送,而是从浏览器缓存中检索。希望这将解决我遇到的延迟加载问题。我已经尝试在我的应用程序的根目录中定义一个 cache.manifest 文件(它定义了要缓存的不同 svgs),但是当组件加载时,服务器仍然在每次组件时发送 svgs(和 param.js)加载。!

这就是我构造一个组件模板的方式,它由 svg 符号组成,当我单击 svg 符号时,我会打开一个应包含相同符号的弹出窗口:

<div>
    <!--Header-->
    <div>{{ name }}</div>

    <!--Actual svg symbol Object-param style-->
    <div class="symbolIcon"(mouseup)="onMouseUp()" (mousedown)="onMouseDown()" >
        <object type="image/svg+xml" data="app/mySymbol.svg" >
                <param name='Activate' value={{activate}} /> 
                <param name='Status' value={{status}} /> 
        </object>
    </div>
</div>

<!--Placeholder for Popup-->
<modal-popup *ngIf="loadPopup" (command)="popupCmd($event);">
    <!--Symbol to be placed on Popup (same as in template above)-->
    <div class="modal-startstop-symbol"> …
Run Code Online (Sandbox Code Playgroud)

html svg caching html5-appcache angular2-template

5
推荐指数
0
解决办法
1286
查看次数

Angular 2 模型函数插值 - self.context.$implicit.X 不是函数

全部,

尝试绑定到模型上的函数时出现错误

我有以下组件、模型和 Html(对于这个例子都是假的)

export class TestModel {    
  public getSomeValue(): string {
     return "Hello World";
  }
}
Run Code Online (Sandbox Code Playgroud)

让我们假设 testModels 属性以某种方式获取它的数据。

@Component(...)
public class MyComponent {    
  public testModels: TestModel[];

  ngOnInit() {
    this.loadData();
  }

  public loadData(): void
  {
     this.http.get("...")
     .map(r=>r.json as TestModel[]) // <--- I think now you've asked the question, I see the problem on this line
     .subscribe(d=>this.testModels = d);

  }
}
Run Code Online (Sandbox Code Playgroud)

现在在我的 html

<div *ngFor="let testModel of testModels">
  <span>{{testModel.getSomeValue()}}</span> <!-- This fails -->
</div>
Run Code Online (Sandbox Code Playgroud)

这是完整的错误:

error_handler.js:48 EXCEPTION: Error in ./MyComponent …

string-interpolation typescript angular2-template angular

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

Angular2:如何在 svg 中添加/更改 css 样式?

我有以下代码将 svg 添加到 angular2 组件的模板:

<object type="image/svg+xml" data="kiwi.svg" class="logo">
  Kiwi Logo
</object>
Run Code Online (Sandbox Code Playgroud)

为了动态添加 css 样式,我尝试了几种方法来获取内容:

1)

public ngAfterViewInit(): void {
        this.el = this._doc.getElementsByTagName('svg');
        console.log(this.el);
    }
Run Code Online (Sandbox Code Playgroud)

结果在控制台:[]

2)

public ngAfterViewInit(): void {
  this.el = this._elementRef.nativeElement.querySelector('object');
  console.log(this.el);
}
Run Code Online (Sandbox Code Playgroud)

结果在控制台:null

题:

任何人都可以帮助我了解如何访问 svg 以及如何更改打字稿中的 css 样式吗?

svg dom typescript angular2-template angular

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

单击按钮时覆盖 routerLink

我有一个我正在制作的电子商务网站,我的目标是每个对象有多个输入点,例如,他们点击/点击卡片上除按钮之外的任何地方,它会在点击/点击的同时转到产品详细信息按钮将该项目添加到购物车。

目前,我可以将其转发到详细信息,即使点击按钮,它也会转发到详细信息。我想让按钮的动作覆盖路由器。

这是我目前的尝试:

<a *ngFor="let item of list"  >
<div class="col-lg-3 col-md-4 col-xs-6">
    <div class="card" [routerLink]="['/detail', item.item_number]">
    <img style ="width:170px;height:150px; display:block;"alt="170x150" src="{{item.item_image}}">

    <div class="card-block">
    <p class="card-text text-muted">{{item.item_name}}</p>
    <p class="card-text">Size:{{item.item_length}}</p>
    </div>

    <div class="container">        
    <button type="button" class="btn btn-primary btn-block" (click)="addItem()">Add<span [routerLink]="['/detail', item.item_number]" [class.disabled]="disabled ? true : null"></span></button>
    </div>
</div>
</div>
</a>
Run Code Online (Sandbox Code Playgroud)

我希望在按钮中嵌套禁用命令会起作用,但情况似乎并非如此。任何想法都会很棒!

angular2-template angular2-routing angular

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

NGX 数据表上的 Angular 2 Pipe ShortDate

我想将日期列格式化为 Angular 2 NGX 数据表中的短日期。

这是 HTML:

<ngx-datatable
  [rows]="toDos"
  [columns]="columns">
</ngx-datatable>
Run Code Online (Sandbox Code Playgroud)

这是组件 TypeScript

import { Component, OnInit } from '@angular/core';
import { IToDo } from '../shared/todo';
import { ToDoService } from '../shared/todo.service';

@Component({
  selector: 'app-todo-list',
  templateUrl: './todo-list.component.html',
  styleUrls: ['./todo-list.component.css']
})

export class TodoListComponent implements OnInit {

  toDos: IToDo[];
  columns = [
    { prop: 'toDoId' },
    { name: 'To Do', prop: 'name' },
    { prop: 'priority' },
    { prop: 'dueDate'  },
    { prop: 'completed' }
  ];

  constructor(private _toDoService: ToDoService) {
  } …
Run Code Online (Sandbox Code Playgroud)

angular2-template angular2-pipe ngx-datatable angular

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

Angular 2:访问组件内html5视频标签的播放和暂停

我是 angular2 的新手,我一直在尝试访问组件内的 html5 视频标签。虽然我使用@ViewChild 装饰器访问视频标签,但我无法访问播放和暂停功能。我的 player.html 文件是

<video id="thisVideo" #videoplayer *ngIf="openPlayer" controls autoplay="autoplay" preload="auto" [poster]="src.thumbnailUrlHighRes" width="640">
<source [src]="src.videoURL" type="video/mp4" />
<p>Your browser does not support the video tag.</p>
</video>
Run Code Online (Sandbox Code Playgroud)

视频的自动播放设置为 true。我需要使用空格键播放和暂停视频,为此我向主机组件添加了事件侦听器。但问题是我无法在事件侦听器中访问视频标签的播放和暂停功能。有人可以给我一些见解吗?

html video typescript angular2-template angular

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

Angular 将字符串传递给组件——有或没有绑定?

我知道有几种方法可以将字符串文字传递给组件:

<component inputField="string"></component>
<component [inputField]="'string'"></component>
<component inputField="{{'string'}}"></component>
Run Code Online (Sandbox Code Playgroud)

它们有区别吗?Angular 是以第二种和第三种方式检查属性的更改而不是第一种检查,还是 Angular 很聪明,它不检查包含字符串文字的属性的任何更改?

angular2-template angular

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

Angular2 Form 使用 setValue 将值重置为 null

抱歉这个问题。这与我的子组件之一有关,该组件在写入表单时拒绝空字符串。

我有一个表单控制订单号。当我尝试这样做时,
this.myForm.controls['orderNo'].setValue = '123';
它工作正常,并且我的视图已使用此值更新。
但问题是,我无法使用 null 或空字符串设置控件。每当我这样做
this.myForm.controls['orderNo'].setValue = null;
或
this.myForm.controls['orderNo'].setValue = '';
没有反映我的观点时。但是我可以看到表单控件包含该空值。(ng.probe($0).componentInstance.myForm.controls['orderNo'].value 返回 null)。它只是没有反映在视图中。

请帮助我一些想法。谢谢你。

angular2-forms angular2-template

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