我很好奇有没有办法在组件中重复使用两次ng-content?或者将它分配给组件构造函数中的变量?
就像是:
@Component({
selector: "component"
})
@View({
template: `<ng-content></ng-content> and again <ng-content></ng-content>`
})Run Code Online (Sandbox Code Playgroud)
我收到此错误 无法读取null的属性'filter'
当我在角度2中应用滤镜时.我的代码是
http://plnkr.co/edit/K46jJsnmHiONuqIsnuzW?p=preview
import {Pipe} from 'angular2/core';
@Pipe({
name: 'sortByName',
pure: false,
})
export class SortByNamePipe {
transform (value, [queryString]) {
// console.log(value, queryString);
return value.filter((student)=>new RegExp(queryString).test(student.name))
// return value;
}
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个组件,其中包含一个带有routerLink属性的元素,我想从使用该组件的模板中设置该属性.当我尝试这样做时,我收到错误消息'无法读取属性'未定义'的路径'.
我的组件看起来链接这个:
INFO-box.component.ts
import { Input, Component } from "@angular/core";
import { ROUTER_DIRECTIVES } from "@angular/router";
@Component({
selector: "info-box",
template: require("./info-box.component.html"),
directives: [
ROUTER_DIRECTIVES
]
})
export class InfoBoxComponent {
@Input() routerLink: string;
@Input() imageUrl: string;
}
Run Code Online (Sandbox Code Playgroud)
INFO-box.component.html
<a [routerLink]="[routerLink]">
<img src="{{imageUrl}}" width="304" height="236">
</a>
Run Code Online (Sandbox Code Playgroud)
并且使用组件的模板看起来像这样:
<info-box [routerLink]="['/test']" [imageUrl]="['./testimage.jpg']"
Run Code Online (Sandbox Code Playgroud)
如果我不添加routerLink一切正常.我的路由器配置似乎是正确的,因为当我直接添加到我的模板时,它也可以正常工作.谁能帮我这个?
Grt Marco
javascript angular2-template angular2-routing angular2-components angular
我有一个自定义angular2管道,它UserInfo使用服务将uid(字符串)转换为对象.
@Pipe({name: 'userInfo'})
export class UserInfoPipe implements PipeTransform {
constructor(public _userService: UserService) {
}
transform(uid:string) : any {
/*let users = this._userService.users.filter((u)=> {
return u.uid==uid;
});
if(users.length==1) return users[0];
return null;*/
return {"Name":"hans","Age":13};
}
}
Run Code Online (Sandbox Code Playgroud)
在我的组件中,我有一个绑定:
<Label row="4" text="Author: {{event?.author | userInfo | .Name}}" class="small-spacing"></Label>
Run Code Online (Sandbox Code Playgroud)
event?.author返回UID,在通过管道后userInfo我有一个对象.但是如何访问该对象的属性?有没有语法?.Name不起作用.
在我的项目中,我有一个包含纬度和经度列表的数据库,对应于几个地址,我想从这个数据库中获取它们并使用Angular 2在Google Maps中绘制它们(带路径).但是,我被卡住了在这个问题.我没有输出.我的代码看起来像这样.
服务组件看起来像这样.
import { Injectable } from '@angular/core';
import {Http, Response, Headers, RequestOptions} from "@angular/http";
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
export class HawkerserviceService{
baseUrl: string;
constructor(private http: Http) {
}
fetchNews(): Observable<any> {
return this.http.get('assets/data/locations.json')
.map(response => response.json());
}
}
Run Code Online (Sandbox Code Playgroud)
我在家庭组件中调用它,看起来像这样
import { Component, OnInit } from '@angular/core';
import {Observable} from 'rxjs/Rx';
import {HawkerserviceService } from '../hawkerservice.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
public latitude: number; …Run Code Online (Sandbox Code Playgroud) 在app.component.html文件中,我有一个带水平滚动的div元素和两个按钮作为Next和Previous.基于这些按钮单击我想移动滚动.
app.component.html
<div id="th-infinite-scroll-tracker" style="overflow-y:scroll; height: 200px;" scrollTracker (scroll)="onScroll($event)" (mouseup)="searchLog($event)">
<div *ngFor="let log of arr; let i = index" innerHTML="{{log}}" [id]="i"></div>
</div>
<button (click)="onPreviousSearchPosition()" [disabled]="disablePreviousBtn">Previous</button>
<button (click)="onNextSearchPosition()" [disabled]="disableNextBtn">Next</button>
Run Code Online (Sandbox Code Playgroud)
app.component.ts
@HostListener('scroll', ['$event'])
onScroll(event){
this.scrollObject = event;
}
onPreviousSearchPosition(){
this.disableNextBtn = false;
this.scrollObject.target.scrollTop = 20 * --this.idCount;
}
onPreviousNextPosition(){
this.disableNextBtn = false;
this.scrollObject.target.scrollTop = 20 * ++this.idCount;
}
Run Code Online (Sandbox Code Playgroud)
使用上面的代码我们可以移动滚动但我们需要滚动事件对象,它将在手动滚动后得到.请建议我,如何在组件类中创建滚动事件对象
我想创建一个通用的html片段组件.我们的想法是将html片段存储在db中的某个地方,以及应该应用于它们的样式.
我可以轻松设置innerHtml以将html结构插入到我的视图模板中的基本元素中,但是如何在我的视图模板中动态插入<style>标记?
这就是我所拥有的:
@Component({
moduleId: module.id,
selector: 'htmlFragment',
styleUrls: ['html-fragment.css'],
templateUrl:'html-fragment.html'
})
export class HtmlFragmentComponent {
@Input() htmlContent: string;
@Input() styleContent: string;
}
Run Code Online (Sandbox Code Playgroud)
这是视图模板:
<style [innerHTML]="styleContent"></style>
<div [innerHTML]="htmlContent"></div>
Run Code Online (Sandbox Code Playgroud)
然后我试着像这样使用它:
<htmlFragment [htmlContent]='dHtml' [styleContent]="sHtml"></htmlFragment>
Run Code Online (Sandbox Code Playgroud)
哪里:
dHtml: string = '<div>hello this is html<ul><li>bla bla</li><li>bla bla 2</li></ul></div>';
sHtml: string = 'ul{list-style-type: none;}';
Run Code Online (Sandbox Code Playgroud)
html片段在这里正确注入:
<div [innerHTML]="htmlContent"></div>
Run Code Online (Sandbox Code Playgroud)
但是这里的样式元素:
<style [innerHTML]="styleContent"></style>
Run Code Online (Sandbox Code Playgroud)
工作不对.有没有办法做到这一点?
请考虑以下代码示例:
<div *ngIf="condition; else elseBlock">
<!-- markup here -->
</div>
<ng-template #elseBlock>
<div>
<!-- additional markup here -->
</div>
</ng-template>
Run Code Online (Sandbox Code Playgroud)
我可以实现相同功能的另一种方法是:
<div *ngIf="condition">
<!-- markup here -->
</div>
<div *ngIf="!condition">
<!-- additional markup here -->
</div>
Run Code Online (Sandbox Code Playgroud)
我想知道应该使用这两种方式中的哪种方式的具体原因以及原因?
我正在研究一个Angular 2项目.我使用ngx-datatable显示用户列表:
<ngx-datatable class="material"
[rows]="rows"
[columnMode]="'force'"
[headerHeight]="40"
[footerHeight]="40"
[rowHeight]="30"
[externalPaging]="true"
[limit]="limit"
(select)='onSelect($event)'
(activate)='onActivate($event)'
[selected]="selected"
[selectionType]="'checkbox'">
<ngx-datatable-column
[width]="30"
[sortable]="false"
[canAutoResize]="false"
[draggable]="false"
[resizeable]="false"
[headerCheckboxable]="true"
[checkboxable]="true">
</ngx-datatable-column>
<ngx-datatable-column name="Name">
<ng-template let-value="value" ngx-datatable-cell-template>
<div class="redNumber">{{value}}</div>
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="Title"></ngx-datatable-column>
<ngx-datatable-column name="company"></ngx-datatable-column>
<ngx-datatable-column name="Status" [cellClass]="getStatusClass">
</ngx-datatable-column>
<ngx-datatable-column name="Last connexion"></ngx-datatable-column>
Run Code Online (Sandbox Code Playgroud)
当我点击触发以下SHOWALL()方法,什么也不会发生,除非我点击第二次按钮,列表被刷新,或者如果我点击一次,调整我的浏览器窗口,列表被刷新.如何能我正确刷新列表?我究竟做错了什么?在我的组件类中,我有这个:
export class M1OamRoomsListComponent{
selected = [];
public rows = [
.... // list of users
];
public limit: number;
constructor(public translation: TranslationService){
this.limit = 10;
}
getStatusClass({ row, column, value }): any {
return { …Run Code Online (Sandbox Code Playgroud) 我有一个模特
class AnnouncementDocument {
Id: String;
IsDeleted: Boolean;
Name: String;
TemporaryName: String;
Description: String;
AnnouncementDocumentTypeId: Number;
}
Run Code Online (Sandbox Code Playgroud)
在我正在使用的模板中
*ngIf="announcementDocumentTypeId==1
Run Code Online (Sandbox Code Playgroud)
我得到了错误
[Angular] Expected the operants to be of similar type or any
Run Code Online (Sandbox Code Playgroud)
正确,因为模板中的1被视为字符串而非数字
我该如何解决这个问题?