相关疑难解决方法(0)

在Angular 2中使用ngForTemplate时绑定事件

假设我有这个简单的列表渲染组件:

import {Input, Component } from 'angular2/core'

@Component({
  selector: 'my-list',
  template: `
      <div *ngFor='#item of items' (click)='onItemClicked(item)'>
          {{item}}
      </div>
  `
})
class MyList {
    @Input() items: string[];

    onItemClicked(item) { console.log('Item clicked:', item); }
}
Run Code Online (Sandbox Code Playgroud)

我这样使用它:

  <my-list [items]='myAppsItems'></my-list>
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.

接下来我决定我希望用户能够为渲染项提供自己的模板,因此我更改了组件

@Component({
  selector: 'my-list',
  template: `
      <template ngFor [ngForOf]="items" [ngForTemplate]="userItemTemplate" (click)='onItemClicked(item)'>
      </template>
  `
})
class MyList {
    @Input() items: string[];
    @ContentChild(TemplateRef) userItemTemplate: TemplateRef;

    onItemClicked(item) { console.log('Item clicked:', item); }
}
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

<my-list [items]='items'>
   <template #item>
        <h1>item: {{item}}</h1>
   </template>
</my-list>
Run Code Online (Sandbox Code Playgroud)

这只适用于我没有将任何事件处理程序绑定到列表项(plunker).如果我尝试绑定到click事件,就像我在组件的第一个版本中所做的那样,Angular会抛出以下异常: …

angular

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

组件使用内联模板进行转换

我正在使用Angular 2-rc3并且Component我想要以一种不同的方式应用转换.这是我的组件:

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

@Component({
    selector: 'my-list',
    template: `<ul>
        <li *ngFor="let item of data">
            -- insert template here --
            <ng-content></ng-content>
        </li>
    </ul>`
})
export class MyListComponent {
    @Input() data: any[];
}
Run Code Online (Sandbox Code Playgroud)

我这样使用它:

<my-list [data]="cars">
    <div>{{item.make | uppercase}}</div>
</my-list>
Run Code Online (Sandbox Code Playgroud)

如您所见,我正在尝试定义将由我的组件使用的内联模板.现在这非常错误.首先,一个数据绑定异常说出来can't read property 'make' of undefined.它试图读取item.make我周围的组件,而不是MyListComponent.但即使我暂时禁用此功能:

<my-list [data]="cars">
    <div>{item.make | uppercase}</div>
</my-list>
Run Code Online (Sandbox Code Playgroud)

然后出现第二个问题:

-- insert template here --
-- insert template here --
-- insert template here --
-- …
Run Code Online (Sandbox Code Playgroud)

angular2-components angular

7
推荐指数
1
解决办法
4015
查看次数

angular2从`[ngTemplateOutlet]`返回"<template>"数据

所以我有一个组件 <template>

<some-component [data]="someArray">
  <template>foo<template>
</some-component>
Run Code Online (Sandbox Code Playgroud)

并且它正在使用它来获取模板

@ContentChild(TemplateRef)
public tmpl: TemplateRef<any>;
Run Code Online (Sandbox Code Playgroud)

然后在它的模板中使用它

<div *ngFor="let item of someArrayFromDataInput">
  <template [ngTemplateOutlet]="tmpl"></template>
</div>
Run Code Online (Sandbox Code Playgroud)

现在我希望能够从item原始模板中打印一些数据,基本上能够做到这一点

<some-component [data]="someArray">
  <template>foo {{ item }}<template>
</some-component>
Run Code Online (Sandbox Code Playgroud)

有可能吗?

angular

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

如何使用EmbeddedViewRef的上下文变量

我有点不确定如何使用EmbeddedViewRefcontext变量.根据我从Angular 2的changelog中收集的内容,该context变量将the setLocalgetLocalmethods 替换为在嵌入视图中设置局部变量的机制.

在查看了这个使用的博客文章后,setLocal我将以下最小的例子拼凑在一起:

import { Directive, TemplateRef, ViewContainerRef } from '@angular/core'

export class FooTemplateContext {
  constructor(public bar: string, public baz: string, public qux: string) {}
}

@Directive({
  selector: '[foo]'
})
export class Foo {
  constructor(viewContainerRef: ViewContainerRef, templateRef: TemplateRef<FooTemplateContext>) {
    let context = new FooTemplateContext('bar', 'baz', 'qux');
    let view = viewContainerRef.createEmbeddedView(templateRef, context);
  }
}
Run Code Online (Sandbox Code Playgroud)
import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'

import { Foo …
Run Code Online (Sandbox Code Playgroud)

angular2-directives angular

4
推荐指数
1
解决办法
3701
查看次数