【发布时间】:2017-03-06 06:52:03
【问题描述】:
基本上,我想要的是传输数据 b/w 组件。
我在母版页上有一个 angular2 组件,用于部分查看登录 -
<loginpartial></loginpartial>
这最初是通过这个带有子组件的html模板呈现的-
<ul [hidden]="!item.isAuthenticated">
<li><a href="javascript:;">Hello! {{item.userName}}</a></li>
<li><a href="javascript:;">LogOff</a></li>
</ul>
<ul [hidden]="item.isAuthenticated">
<li><a href="javascript:;">Hello! User</a></li>
<li><a href="javascript:;">Log In</a></li>
</ul>
子组件 -
@Component({
selector: 'loginpartial',
templateUrl: '../Templates/LoginPartial.html'
})
export class LoginPartialComponent implements OnInit {
public item: any = { isAuthenticated: false, userName: ' ' }
constructor() {
this.item.isAuthenticated = false;
}
ngOnInit() {
this.item.isAuthenticated = false;
}
}
我希望这直到现在才有意义。现在我需要的是,一旦用户登录,我想在这个从父组件传输的局部视图中获取用户名。
父组件
import { Component, ChangeDetectionStrategy, OnInit} from '@angular/core';
import {LoginPartialComponent} from '../loginpartialview/loginpartial.component.ts';
import {SharedService} from '../sharedService/app.sharedService.ts';
@Component({
selector: 'loginmodel',
templateUrl: '../Templates/Login.html',
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [LoginPartialComponent,SharedService]
})
export class LoginComponent implements OnInit {
item: any = {
isAuthenticated: false, userName: ' '
};
constructor(private sharedService: SharedService,
private loginComp: LoginPartialComponent) {
}
onSubmit(item: any): void {
if (this.myForm.valid) {
var userObject = {
email: item.email,
password: item.password
};
this.sharedService.getLogin(userObject).map(response => response.json())
.subscribe(
data => this.handleResult(data),
error => console.log(error)
);
}
}
private handleResult(data) {
if (data.success) {
this.item.isAuthenticated = true;
this.item.userName = data.userName;
this.item = this.loginComp.item;
}
else {
}
}
}
从这个父组件中可以看出,当调用onSubmit 函数时,这是我的登录表单提交,我正在调用handleResult,这是从共享服务的ajax 调用设置数据。
这不会让子组件 - LoginPartialComponent 刷新。我也尝试使用 @Input() 类型参数,但没有帮助。
更新 -
正如@Camaron 在他的回答中所说,我仅使用事件发射器为此登录目的创建了一个依赖项。但这不起作用。
LoginPartialComponent
import { Component, OnInit} from '@angular/core';
import {LoginPartialModel} from '../loginpartialview/loginpartial.model.ts';
import {SharedService} from '../sharedService/app.sharedService.ts';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Component({
selector: 'loginpartial',
templateUrl: '../Templates/LoginPartial.html',
providers: [SharedService]
})
export class LoginPartialComponent {
item: LoginPartialModel = new LoginPartialModel();
constructor(private service: SharedService) {
this.service.onLoginSuccess.subscribe((loginData: any) => this.item == loginData.item);
}
}
登录组件
import { Component, OnInit} from '@angular/core';
import {SharedService} from '../sharedService/app.sharedService.ts';
@Component({
selector: 'loginmodel',
templateUrl: '../Templates/Login.html',
providers: [SharedService]
})
export class LoginComponent {
constructor(private sharedService: SharedService) {
}
onSubmit(item: any): void {
var userObject = {
email: item.email,
password: item.password
};
this.sharedService.getLogin(userObject).map(response => response.json())
.subscribe(
data => this.handleResult(data),
error => console.log(error)
);
}
private handleResult(data) {
if (data.success) {
this.close();
this.sharedService.onLoginSuccess.emit(data);
}
else {
}
}
}
共享服务(依赖)
import { Component, Injectable, EventEmitter} from '@angular/core';
@Injectable()
export class SharedService {
onLoginSuccess: EventEmitter<any> = new EventEmitter<any>();
constructor() {
}
notifyLoginSuccess(item: any): void {
this.onLoginSuccess.emit({ item: item });
}
}
这不起作用。
【问题讨论】:
-
在你的
handleResult函数内部,不应该是this.loginComp.item = this.item;而不是this.item = this.loginComp.item;? -
@camaron,是的,应该是这个。但这不会改变它。
-
您是否尝试从两个组件中删除 changeDetection 策略?
-
@camaron,实际上这些组件没有嵌套。两者都是独立的。您认为删除 changeDetection 会有所帮助吗?
-
检查这一行
this.service.onLoginSuccess.subscribe((loginData: any) => this.item == loginData.item)有一个双 = 应该只有一个 =。
标签: angularjs angular typescript angularjs-directive angularjs-scope