标签: angular2-template

在Angular 2模板中键入检查

我们正在使用Angular 2和TypeScript构建应用程序.我们尝试静态检查可能的类型.有没有办法检查模板中的类型?请考虑以下片段:

<foo [data]="dataObj"></foo>
Run Code Online (Sandbox Code Playgroud)

假设dataFoo组件中有某种类型TData.但是,默认情况下,没有什么能阻止我传递dataObj不符合的内容TData.是否有Angular模板的typescript扩展,可以在这种情况下验证类型?

typescript angular2-template angular

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

使用带有<router-outlet>的@Inputs的Angular2

我的页面中有一个子导航,在公共主视图下方显示一些子视图.我想通过一个对象传递给子视图,<router-outlet>这样我就可以在主组件中检索一次数据,然后与子组件共享它.

注意:如果我<one></one>main.html中包含该指令,它可以工作,但这不是我想要的行为.

主要观点:

<h1>Details</h1>   
<a [router-link]="['./sub1']">One</a> | 
<a [router-link]="['./sub2']">Two</a> | 
<a [router-link]="['./sub3']">Three</a>   
<hr/>  
<router-outlet [data]="maindata"></router-outlet>
Run Code Online (Sandbox Code Playgroud)

子视图1:

<h2>{{ data.name }}</h2>
...
Run Code Online (Sandbox Code Playgroud)

主要观点:

@Component({
    selector: 'main-detail',
    directives: [ROUTER_DIRECTIVES],
    templateUrl: './main.html'
})
@RouteConfig([
    { path: '/', redirectTo: '/one' },
    { path: '/one', as: 'One', component: OneComponent },
    { path: '/two', as: 'Two', component: TwoComponent },
    { path: '/three', as: 'Three', component: ThreeComponent }
])
export class MainComponent {
    maindata: Object = {name:'jim'};
}
Run Code Online (Sandbox Code Playgroud)

子视图1:

@Component({
    selector: 'one', …
Run Code Online (Sandbox Code Playgroud)

typescript angular2-template angular

43
推荐指数
2
解决办法
4万
查看次数

将[NgStyle]与条件组合(if..else)

我已经阅读了Angular2的NgStyle文档.对我来说有点困惑.关于如何使用NgStyle与条件(if ... else)来设置任何元素的背景图像的任何想法?

angular2-template angular

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

得到插值({{}}),其中表达式是预期的

我有以下HTML但我得到了例外.怎么解决?

解析器错误:插值({{}})其中表达式位于[!(editForm.controls.field_item_exportExpression _ {{i}} ?. dirty && editForm.controls.field_item_exportExpression _ {{i}}?无效的第48列) ]

<div class="form-group">
  <label class="form-control-label" for="field_exportExpression">exportExpression</label>
  <input class="form-control" type="text" id="field_item_exportExpression_{{i}}" name="item_exportExpression_{{i}}" [(ngModel)]="datatype.items[i].exportExpression" required>
  <div [hidden]="!(editForm.controls.field_item_exportExpression_{{i}}?.dirty && editForm.controls.field_item_exportExpression_{{i}}?.invalid)">
   <small class="form-text text-danger" [hidden]="!editForm.controls.field_item_exportExpression_{{i}}?.errors?.required" dpTranslate="dataconfiguration.validation.required"> This field is
                            required. </small>
 </div>
</div>
Run Code Online (Sandbox Code Playgroud)

以下不起作用.说[找到不需要的令牌.

[hidden]="!editForm.controls.['item_exportExpression_' + i]?.errors?.required 
Run Code Online (Sandbox Code Playgroud)

以下不是抱怨[而是抱怨Cannot read property '0' of undefined

 [hidden]="!editForm.controls.item_exportExpression_[ i]?.errors?.required 
Run Code Online (Sandbox Code Playgroud)

angular2-forms angular2-template angular

40
推荐指数
4
解决办法
4万
查看次数

为什么*ngIf与ng-template无关?

我在模板中有一个条件如下:

<ng-container>
    <p *ngFor="let seat of InfoDetails?.seatInfo">
        <template *ngIf="seat.section">
            Section {{seat?.section}} ,
        </template>
        <template *ngIf="seat.row">
            Row {{seat?.row}},
        </template>
        <template *ngIf="seat.seatNo">
            Seat number {{seat?.seatNo}}
        </template>
    </p>
</ng-container>
Run Code Online (Sandbox Code Playgroud)

我有包含row和的数据集seatNo,但它似乎没有在模板中打印.这是什么问题?

angular2-template ng-template angular

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

Angular2 SVG xlink:href

我有用于渲染SVG图标的组件:

import {Component, Directive} from 'angular2/core';
import {COMMON_DIRECTIVES} from 'angular2/common';

@Component({
  selector: '[icon]',
  directives: [COMMON_DIRECTIVES],
  template: `<svg role="img" class="o-icon o-icon--large">
                <use [xlink:href]="iconHref"></use>
              </svg>{{ innerText }}`
})
export class Icon {
  iconHref: string = 'icons/icons.svg#menu-dashboard';
  innerText: string = 'Dashboard';
}
Run Code Online (Sandbox Code Playgroud)

这会触发错误:

EXCEPTION: Template parse errors:
  Can't bind to 'xlink:href' since it isn't a known native property ("<svg role="img" class="o-icon o-icon--large">
<use [ERROR ->][xlink:href]=iconHref></use>
  </svg>{{ innerText }}"): SvgIcon@1:21
Run Code Online (Sandbox Code Playgroud)

我该如何设置动态xlink:href

angular2-template angular

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

如何从组件模板中将数组作为Input()传递?

我需要使用绑定将值数组传递给组件,例如

@Component({
    selector: 'my-component',
    template: '<div data="[1, 2, 'test']"></div>
})
export class MyComponent {
    @Input() data: any[];
    ...
}
Run Code Online (Sandbox Code Playgroud)

但是,似乎Angular将其视为string/ string[1](在实际项目中,数组是一个路由,我需要将此路由传递给具有该[routerLink]指令的组件).

我该怎么做?

angular2-template angular

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

angular2:错误:TypeError:无法读取未定义的属性"..."

我附上了我的angular2代码片的plunker.我想从我的JSON打印一个字段但是无法打印,因为最初我的Object是null并且它是通过Promise填充的.

这是我的组件文件

import {Component, NgModule, OnInit} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'

class MyData {
  xyz : MySubData;
}

class MySubData {
  name : string;
} 
@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>

      {{abc.xyz.name}}
    </div>
  `,
})
export class App implements OnInit {
  abc : MyData = null;
  constructor() {
    this.name = 'Angular2'
  }

  ngOnInit() {
    setTimeout(() => {
      this.abc = new MyData();
      this.abc.xyz = new MySubData();
      this.abc.xyz.name = "Binita";
    }, 2000);
  }
}

@NgModule({
  imports: [ BrowserModule ], …
Run Code Online (Sandbox Code Playgroud)

2-way-object-databinding angular2-template angular

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

如何在Angular 2中的同一组件中使用多个ng-content?

我想在我的组件中显示不同的模板.只有一个会显示.如果hasURLtrue,我想表明<a></a>.如果hasURLfalse,我想表明<button></button>.

问题如果hasURL为false,组件显示按钮,但ng-content为空.因为它已经在第一篇中读到了"a></a>

有没有办法解决这个问题?

        <a class="bouton" href="{{ href }}" *ngIf="hasURL">
            <ng-content>
            </ng-content>
        </a>

        <button class="bouton" *ngIf="!hasURL">
            <ng-content>
            </ng-content>    
        </button>
Run Code Online (Sandbox Code Playgroud)

谢谢!

javascript angular2-directives angular2-template angular

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

角度2一次装订

在角度1中,我们可以用这种方式进行一次绑定:{{ ::myFunction() }}.

在角度2中,这是投掷:

EXCEPTION: Template parse errors:
Parser Error: Unexpected token : at column 2 in [{{ ::consent(false, undefined, box) }}] in CookieConsent@5:29 ("ull-right" href="" (click)="consent(true, $event, box)">De acuerdo</a>
        <span class="hidden">[ERROR ->]{{ ::consent(false, undefined, box) }}</span>
Run Code Online (Sandbox Code Playgroud)

我们如何在angular2中进行一次绑定?

javascript angular2-template angular

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