我目前有一个Angular 2应用程序正在运行,如下所示:
访问该站点时,App.component被引导.App.component的模板具有所有组件标记(例如menu.component,search.component和router-outlet).
我基本上需要的是:目前访问者被直接重定向到登录页面,因为用户需要登录.他仍然能够看到菜单和仅适用于登录用户的所有组件.添加额外模板层的最佳策略是什么,因此未登录的用户会被重定向?
我有一个Angular 2组件,用于*ngFor渲染嵌套的数字数组.
@Component({
selector: 'app',
template: `
<div *ngFor="let row in data">
<div *ngFor="let curve in row">
<chart [data]="curve">
</div>
</div>
`,
directives: [Chart],
})
export default class App {
data: number[][][];
}
Run Code Online (Sandbox Code Playgroud)
当我改变时data,<chart>即使新数组具有相同的尺寸,Angular 也会替换元素.我只想更新图表的属性但保留元素(以便我可以设置数据变化的动画).
可以理解,Angular会替换元素,因为新数组是一个新的对象引用.但我怎么能绕过这个呢?
我正在使用带有Typescript的Angular2
假设我的app组件模板中有以下内容:
...
<coffee-cup [coffee]=""
...
Run Code Online (Sandbox Code Playgroud)
我的coffee-cup组件:
@Component({
selector: 'coffee-cup',
...
})
export class CoffeeCup {
@Input()
public coffee = 0;
}
Run Code Online (Sandbox Code Playgroud)
我目前不确定我的输入应该是什么样子.它可能看起来像这样:
@Input()
public coffee = 0;
Run Code Online (Sandbox Code Playgroud)
要么
@Input()
private coffee = 0;
Run Code Online (Sandbox Code Playgroud)
我目前倾向于将成员变量咖啡私有化.
我查看组件的方式是有两个单独的API与之交互:
@Inputs和组成@Outputs我没有检查在下列情况下发生了什么,但是,它可能会改变答案:
appComponent能存取CoffeeCup使用@ViewChild,并设置咖啡成员,将生命周期挂钩(像ngOnChange)火?重申一下这个问题:Angular2应该@Input是公开的,还是应该通过将它们设为私有来实现更严格的API?
typescript angular2-template angular2-inputs angular2-components angular
我使用模板驱动的方法在Angular 2中构建表单,我已经成功创建了可以在模板中使用的自定义验证器.
但是,我找不到一种方法来显示绑定到特定错误的特定错误消息.我想区分为什么表单无效.我怎么做到这一点?
import { Component } from '@angular/core';
import { NgForm } from '@angular/forms';
import { Site } from './../site';
import { BackendService } from './../backend.service';
import { email } from './../validators';
import { CustomValidators } from './../validators';
@Component({
templateUrl: 'app/templates/form.component.html',
styleUrls: ['app/css/form.css'],
directives: [CustomValidators.Email, CustomValidators.Url, CustomValidators.Goof],
providers: [BackendService]
})
export class FormComponent {
active = true;
submitted = false;
model = new Site();
onSubmit() {
this.submitted = true;
console.log(this.model);
}
resetForm() {
this.model = new Site();
this.submitted = …Run Code Online (Sandbox Code Playgroud) typescript angular2-forms angular2-directives angular2-template angular
我正在开发基于版本桌面/移动版的略有不同功能的网站.
我尝试通过@View应用它,但看起来这个装饰器现在已被弃用.请告诉我最佳实践,如何在Angular 2中实现此功能.
我感兴趣的是如何TemplateRef通过传递一个string例子来获得一个.
我不想通过组件html获取它,因为我只想将它保存在配置中以用于所有组件.我正在引用NgbDatePickerConfig可以在这里查看的ng-bootstrap :https://github.com/ng-bootstrap/ng-bootstrap/blob/master/src/datepicker/datepicker-config.ts.
在角度1我能够做类似的事情:
<script type="text/ng-template" id="commentView.html">
<div>
<div style="float: left; position: absolute;" ng-style="comment.displayPercent">
</div>
</script>
<script type="text/ng-template" id="commentReplies.html">
<div>
<div class="commentChildBoxStyle" ng-hide="comment.hideComment">
<div style="min-width: 250px;">
<div ng-repeat="comment in comment.replies" ng-include="'commentReplies.html'"></div>
</div>
</div>
</div>
</script>
Run Code Online (Sandbox Code Playgroud)
我意识到我可以在Angular2中递归调用一个组件及其模板,但有没有办法只递归构建HTML?逻辑似乎更适合父组件而不是一系列子组件.
我使用innerHTML在我的组件中呈现HTML内容.但是innerHTML删除HTML内容中的所有属性(例如:删除样式属性)并呈现它.但需要按原样呈现,并且不希望从HTML内容中提取任何属性.是否有任何与innerHTML等效的内容,或者我们是否可以使用innerHTML完成所需的结果.提前致谢.
我的模板文件
<div id="more-info-box" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Title</h4>
</div>
<div class="modal-body">
<div class="clearfix">
<div [innerHTML]="htmlContent" class="long-desc-wrap">
</div>
</div>
</div>
</div>
</div>
</div><!-- End Info box -->
Run Code Online (Sandbox Code Playgroud)
我的组件文件
import { Component } from '@angular/core';
@Component({
selector: 'cr-template-content',
templateUrl: './app/channel.component.html'
})
export class TemplateComponent {
constructor() {
}
htmlContent = '<p>This is my <strong style="color:red">Paragraph</strong></p>'
}
Run Code Online (Sandbox Code Playgroud)
我当前的输出是没有样式属性的内容.但是期望的结果应该是style属性.
因此,我想在特定页面被击中时使我的Header上的锚元素消失.当该页面被击中时,如何在*ngIf中捕获url.
我有一个标题,所有页面都保持相同.当我在/ home路由时,只需要隐藏一个锚元素.如何在*ngIf中捕获这个"/ home"?
*ngIf ="href ='/ home'"无效.任何替代品?
@Component({
selector: 'bancaComponent',
templateUrl: '{{str}}'
})
export class BancaComponent implements OnInit {
str: String;
constructor(private http: Http) { }
ngOnInit(): void {
this.str = "./file.component.html";
}
Run Code Online (Sandbox Code Playgroud)
还有另一种方法吗?谢谢 :)