我在typescript中有这个组件定义:
import {Component, ViewEncapsulation} from 'angular2/core';
@Component({
selector: 'my-app',
templateUrl: '/views/sandbox.html',
styleUrls: ['/styles/sandbox.css'],
styles: [`.wow { background-color: red; }`],
encapsulation: ViewEncapsulation.Emulated
})
export class SandBox { }
Run Code Online (Sandbox Code Playgroud)
根据这篇文章:http://blog.thoughtram.io/angular/2015/06/25/styling-angular-2-components.html 无论是在样式部分和外部样式表的样式应该内联到页眉通过角度.
不幸的是,第二个没有被注入,角度仅注入样式部分中的一个.
我试过从浏览器访问/styles/sandbox.css,没关系.Angular也可以访问/views/sandbox.html,所以我不知道它为什么没有发生.
我正在使用:"angular2":"2.0.0-beta.2"(最新测试版AFAIK)
在我的应用程序中,有条件地我正在添加一个类.当用户输入的东西我正在检查值,因此我添加了类名.它工作正常.
但它只更新一组(keyup)='0'- 设置一些值keyup.这不像angular 1这里.
所以任何人都解释我为什么要设置(keyup)=0这里?它为我们做了什么?
这是我的代码:
import {Component} from "angular2/core"
@Component({
selector : 'my-component',
template : `
<h2>My Name is: {{name}}
<span [class.is-awesome]="formReplay.value === 'yes' ">So good</span>
</h2>
<input type="text" #formReplay (keyup)="0" />
`,
styles : [`
.is-awesome{
color:green;
}
`]
})
export class MyComponent {
name = "My Name";
}
Run Code Online (Sandbox Code Playgroud) 我有一个angular2应用程序,其后端是在java中.我有一份客户名单.当我单击以删除客户时,客户将被删除,但列表不会更新.如果我手动刷新页面,则列表会更新.我尝试在delete方法的订阅中路由到列表组件,但这不起作用.
列表customers.component.html
<tr [class.warning]="customer.isDefault == 1" *ngFor="let customer of customers | orderBy:['firstName'] | search:searchCustomer.value;let serial = index">
<td>{{ serial+1 }}</td>
<td>{{ customer?.firstName+' '+customer?.lastName}}</td>
<td>{{ customer.email}}</td>
<td>{{ customer.mobileNumber}}</td>
<td>{{ customer.streetAddress}}</td>
<td>{{ customer.priceList?.name}}</td>
<td><a [routerLink]="['/loggedIn','customer','edit', customer.id ]"><i class="fa fa-edit fa-2x"></i></a></td>
<td><a (click)="delete(customer)"><i class="fa fa-trash-o fa-2x"></i></a></td>
</tr>
Run Code Online (Sandbox Code Playgroud)
列表customers.component.ts
ngOnInit()
{
this.refreshCustomersList();
}
delete(customer)
{
this.userService.delete(customer.id)
.subscribe(
success=>
{
var index = this.customers.indexOf(customer, 0);
if (index > -1)
{
this.customers.splice(index, 1);
}
}
)
}
refreshCustomersList()
{
this._authHttp.get(
this.appService.getApiUrl() + "api/customer/list"
)
.map(res=>res.json())
.subscribe( …Run Code Online (Sandbox Code Playgroud) 我无法理解为什么ViewChild当相同的确切代码在textarea元素上工作时返回null ,但它不起作用div
模板:
<div #refId class = 'line_counter' >
{{lineNumber}}
</div>
Run Code Online (Sandbox Code Playgroud)
零件:
import {Component, OnInit, Input, ElementRef, ViewChild} from '@angular/core';
import {Globals} from "../../../globals";
@Component({
selector: 'app-line-counter',
templateUrl: './line-counter.component.html',
styleUrls: ['./line-counter.component.css']
})
export class LineCounterComponent implements OnInit {
@ViewChild('#refId') textDiv:ElementRef;
@Input() lineNumber : number = 0;
constructor() {
}
ngOnInit() {
if(Globals.refLineCounter == null) {
Globals.refLineCounter = this;
//console.log(Globals.refLineCounter.getHeight());
console.log(this.getHeight());
}
}
getHeight(){
return this.textDiv.nativeElement.height;
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,在某些情况下我在父路由的子路由之间路由.一个看起来像example.com/a/1/另一个example.com/a/2/将切换1到2触发页面的重新加载/渲染a?
就像播放视频a并将路径出口作为该视频的兄弟一样,此页面更改是否会触发视频重新加载?
如果是这样,可以预防吗?
我的目标是创建子组件并插入父组件模板.有一些例子可以做到这一点.但是,我在父组件中动态创建父组件模板(DOM元素),而大多数示例静态创建带有capture元素的模板.
这是代码
app.component
import {Component, ViewChild, ViewContainerRef, ComponentFactoryResolver} from '@angular/core';
import {NewChildComponent} from "./newChild.component";
@Component({
selector: 'app-main',
templateUrl: 'app.component.html'
})
export class AppComponent {
@ViewChild('captureElement', {read: ViewContainerRef})
captureElement: ViewContainerRef;
constructor (private componentFactoryResolver: ComponentFactoryResolver) {
var childComponent = this.componentFactoryResolver.resolveComponentFactory(NewChildComponent);
var myArea = document.getElementById('myArea');
var myRow = document.createElement("div");
myArea.appendChild(myRow);
//I want to add the capture element #myCapture as a child of myRow
//Add something like this <div #captureElement></div> programmatically through JS/TS
// How can I do this?
// I then …Run Code Online (Sandbox Code Playgroud) 我有一个异步/ http调用,并且如果异步调用导致错误,则希望将焦点设置为模板中的错误元素.
class MyComponent {
constructor(private element: ElementRef) {}
doAsync() {
// do something async
$(this.element.nativeElement).find('.error').focus();
}
}Run Code Online (Sandbox Code Playgroud)
在角度2中包含/使用JQuery有多好?
另外,我认为Angular的工作方式(MV*)是" 修改视图反应的模型 ".那么,哪一个是正确的方法呢?
我们现在可以有else片段,指的是<ng-template>:
<div *ngIf="condition; else not">
Condition satisfied.
<ng-template #not>
Condition not satisfied.
</ng-template>
</div>
Run Code Online (Sandbox Code Playgroud)
我想是能够引用片段的上下文之外*ngIf--something像
<div>DIV1 <ng-template-call template="shared"></ng-template-call></div>
<div>DIV2 <ng-template-call template="shared"></ng-template-call></div>
<ng-template #shared>This is a shared snippet.</ng-template>
Run Code Online (Sandbox Code Playgroud)
写ng-template-call上面我所说的正确方法是什么?
是的,我知道我可以把它变成一个单独的组件,但它没有达到那个水平.我总是可以写:
<div *ngIf="false; else shared">
Run Code Online (Sandbox Code Playgroud)
但这看起来很笨拙.
我正面临着这个令人沮丧的问题
没有指令将"exportAs"设置为"ngForm"("
] #f ="ngForm"(ngSubmit)="onSubmit(f)"class ="form-horizontal">"):ng:/// ComponentsModule/EquipeComponent.html@8:12错误:模板解析错误:没有指令将"exportAs"设置为"ngForm"("] #f ="ngForm"(ngSubmit)="onSubmit(f)"class ="form- horizontal">"):ng:///ComponentsModule/EquipeComponent.html@8:12
这是我的app.module.ts包含的内容
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
import { NgForm } from '@angular/forms';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { BsDropdownModule } from 'ngx-bootstrap/dropdown';
import { TabsModule } from 'ngx-bootstrap/tabs';
import { NAV_DROPDOWN_DIRECTIVES } from './shared/nav-dropdown.directive';
import { ChartsModule } from 'ng2-charts/ng2-charts';
import { SIDEBAR_TOGGLE_DIRECTIVES } …Run Code Online (Sandbox Code Playgroud) angular2-forms angular2-directives angular2-template angular
我是Angular 4的新手.根据我的理解,@ Input用于将值传递给组件.但是当我按照下面提到的那样使用它时它不起作用.
my-file.component.html
<h1 [user] = "currentuser"></h1>
my-file.component.ts
@Input()
user : string;
Run Code Online (Sandbox Code Playgroud)