使用setDate方法修改的日期对象未在模板中更新.
在模板中:
<p>{{date | date:'mediumDate'}}</p>
Run Code Online (Sandbox Code Playgroud)
在组件中:
nextDay(){
this.date.setDate(this.date.getDate()+1);
}
Run Code Online (Sandbox Code Playgroud)
但是当我调用nextDay函数时,模板不会使用新值更新.
我可以让变更检测工作的唯一方法是这样做:
nextDay(){
var tomorrow = new Date();
tomorrow.setDate(this.date.getDate()+1);
this.date = tomorrow;
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来完成同样的任务?
我按照angular.io入门项目使用种子结构.到目前为止一切都还可以.
现在我想更改顶部组件以从分离的文件中获取视图,然后我遇到了麻烦.工作代码是:
import {Component, View} from 'angular2/core';
@Component({
selector: 'my-app',
template: '<h1>Angular2</h1>',
directives: []
})
export class AppComponent {
constructor() {
}
}
Run Code Online (Sandbox Code Playgroud)
然后我将模板更改为templateUrl,如下所示:
templateUrl: 'app.component.html',
我在这个文件中的代码与以前完全相同
<h1>Angular2</h1>
工作似乎很明显,但事实并非如此.它没有吐出错误,但除了"loading ...."消息之外,它没有显示任何内容,如在入门演示中那样
谢谢你的帮助
彼得
我正在尝试基于click事件将类应用于HTML元素.从父组件的模板设置子组件的选择器的类属性时,这可以正常工作,如下面的父组件片段所示:
[class.bordered]='isSelected(item)'
Run Code Online (Sandbox Code Playgroud)
这将适当地设置单击该项目时的样式.但是,我想基于相同类型的click事件在子组件中设置内部HTML元素的类,这里是子组件样式的所需目标:
template: `
<div class="This is where I really want to apply the style">
{{ item.val }}
</div>
`
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点,很容易支持?或者这被认为是一种不好的做法,我应该设计我的组件以避免这种条件样式的情况?
完整代码:
@Component({
selector: 'parent-component',
directives: [ChildComponent],
template: `
<child-component
*ngFor='#item of items'
[item]='item'
(click)='clicked(item)'
[class.bordered]='isSelected(item)'>
</child-component>
`
})
export class ParentComponent {
items: Item[];
currentItem: item;
constructor(private i: ItemService) {
this.items = i.items;
}
clicked(item: Item): void {
this.currentItem = item;
}
isSelected(item: Items): boolean {
if (!item || !this.currentItem) {
return false;
}
return item.val === this.currentItem.val; …Run Code Online (Sandbox Code Playgroud) 处理Angular 2中的奇怪问题.
如果查看下面的组件,该服务将返回一个已解析的对象 - 例如我可以console.log.您可以看到粘贴为注释的输出.每当我尝试在视图中使用所述对象时,我都会收到错误:EXCEPTION: TypeError: Cannot read property 'subject' of undefined in [{{theData.subject}} in SubjectHomeComponent@2:27].
这对我来说没有意义,因为我可以在控制台中看到对象就好了.运行typeof也会返回它是一个对象.
组件代码
import { Component, View } from 'angular2/core'
import { CORE_DIRECTIVES } from 'angular2/common'
import {SubjectService} from "../../services/subject/SubjectService.ts";
import Rx from 'rxjs/Rx'
import { Response } from 'angular2/http'
import {ROUTER_PROVIDERS} from "angular2/router";
import {RouteParams} from "angular2/router";
@Component({
selector: 'subjectHomeComponent',
providers: [SubjectService]
})
@View({
template: `
Hello World <span>{{theData.subject}}</span>
`
})
export class SubjectHomeComponent{
constructor(subjectService: SubjectService, params: RouteParams){ …Run Code Online (Sandbox Code Playgroud) 所以我昨天回答了一个问题,我觉得我在复制代码并且没有完全理解我在做什么,直到我开始深入了解Observables.我的问题是我总是在应用和查找文档以满足我的需求时遇到了一些困难.这意味着我仍然非常依赖于了解其他人如何使用代码,因此我可以更好地掌握这些概念.
我花了大部分时间在相当优秀的文档中阅读这篇文章https://github.com/Reactive-Extensions/RxJS/tree/master/doc
我开始理解可观察者对承诺的想法和力量.到目前为止,我有三个问题;
我的其他问题确实涉及如何在我的代码中更有效地使用它们.
我将为接下来几个问题发布我的代码
@Component({
selector: 'my-app',
providers: [FORM_PROVIDERS, RoleService, UserService],
inputs: ['loggedIn'],
directives: [ROUTER_DIRECTIVES, CORE_DIRECTIVES, LoggedInRouterOutlet],
template: `<body>
<div *ngIf="loggedIn[0]=== 'true'">
<nav class="navbar navbar-inverse navbar-top" role="navigation">
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class = "nav navbar-nav">
<li>
<a [routerLink] = "['/RolesList']">Roles</a>
</li>
<li>
<a [routerLink] = "['/UserList']">Users</a>
</li>
<li>
<a [routerLink] = "['/UserRolesList']">User Roles</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a [routerLink] = "['/Login']" (click)="logout()">Logout</a>
</li>
</ul>
</div>
</div>
</nav>
</div>
<div class="container">
<router-outlet></router-outlet>
</div> …Run Code Online (Sandbox Code Playgroud) 假设我从angular2应用程序生成的html如下所示:
<app>
<form [formGroup]="myForm" (ngSubmit)="onSubmit(myForm.value)">
<panel-component>
<mid-component>
<inner-component-with-inputs>
<input/>
<inner-component-with-inputs>
<mid-component>
</panel-component>
<panel-component>
<mid-component>
<inner-component-with-inputs>
<input/>
<inner-component-with-inputs>
<mid-component>
</panel-component>
<!-- many many many fields -->
<button type="submit">Submit</button>
</form>
</app>
Run Code Online (Sandbox Code Playgroud)
如何设置我的外部<form>以便我可以在提交时验证所有内部输入?我必须通过myForm通过@Input()从一路下跌panel-component到inner-component-with-inputs?或者还有其他方式吗?
在我的应用程序中,我有一个非常大的形式,有多个面板,子面板,标签,模态等.我需要能够在提交时一次性验证它.
互联网上的所有教程和资源仅涉及跨越一个组件/模板的表单.
我正在尝试将base64字符串渲染到<img src='data:image/png;base64,${Here}'.
但总是当我尝试渲染它时,ng2在渲染之前清理我的base64字符串,然后在DOM中显示它之前将一些内容添加到我的值中.我找到了解决方法(使用DomSanitizer),但它不适用于最新版本.
这是我的标记:
<img alt="RegularImage" src="data:image/png;base64,{{imgBase64}}">
Run Code Online (Sandbox Code Playgroud)
这是我的组成部分:
imgBase64="SomeBase64StringFetchedSomehow";
Run Code Online (Sandbox Code Playgroud)
但angular2显示在控制台的下一条消息中 - WARNING: sanitizing unsafe URL value
如何防止NG2清理我的base64字符串?
更新
get getImg() {
return this._sanitizer.sanitize(SecurityContext.URL,`data:image/png;base64,${this.img}`);
}
Run Code Online (Sandbox Code Playgroud)
不解决这个问题.在RC6中不再存在DomSanitizer类
我知道在ng2中我们ComponentFactoryResolver可以解决我们可以申请的工厂ViewContainerRef.
但是,指令有类似之处吗?一种实例化它们并将它们应用于组件中的投影内容的方法?
我有这样的代码
<template *ngIf='mobile'>
<div class='wrapper'>
<div class='nav'>
<ng-content></ng-content>
</div>
</div>
</template>
<template *ngIf='desktop'>
<ng-content></ng-content>
</template>
Run Code Online (Sandbox Code Playgroud)
但是,Angular 2只渲染了一次ng-content.有没有办法让这个案子在没有太多黑客攻击的情况下正常工作?
是否有任何Angular Material树视图可以使用Angular 2 or 4哪个应该是递归树并且应该是可自定义的.请一些人请指向好的样本帖子或提供示例代码.
angular ×10
javascript ×2
typescript ×2
date ×1
node.js ×1
observable ×1
projection ×1
rxjs ×1
sanitization ×1