在角度应用程序中,我们有ngOnDestroy()
一个组件/指令的生命周期钩子,我们使用这个钩子取消订阅observables.
我想清除/ @injectable()
服务中创建的destory observable .我看到一些帖子说ngOnDestroy()
可以在服务中使用.
但是,这是一个很好的做法,只有这样才能这样做,什么时候会被召唤?有人请澄清一下.
我有一个使用角度十进制管道的自定义十进制格式管道.此管道是共享模块的一部分.我在功能模块中使用它,并在运行应用程序时没有提供程序错误.
如果有任何拼写错误,请忽略.
./src/pipes/custom.pipe.ts
import { DecimalPipe } from '@angular/common';
..
@Pipe({
name: 'customDecimalPipe'
})
...
export class CustomPipe {
constructor(public decimalPipe: DecimalPipe) {}
transform(value: any, format: any) {
...
}
Run Code Online (Sandbox Code Playgroud)
./modules/shared.module.ts
import { CustomPipe } from '../pipes/custom.pipe';
...
@NgModule({
imports: [ .. ],
declarations: [ CustomPipe ],
exports: [ CustomPipe ]
})
export class SharedModule { }
Run Code Online (Sandbox Code Playgroud)
我在其中一个组件中注入自定义管道并调用transform方法来获取转换后的值.共享模块导入到功能模块中.
我正在尝试构建一个结构指令,它将通过使用其选择器(静态)或通过调用其公共方法(动态)来调用父DOM结构.
在html模板中使用指令选择器可以正常工作而不会出现任何问题.
我试图弄清楚我们是否在模板中使用它并通过调用指令方法实现相同.
我-directive.ts
@Directive({ selector: '[sampleDirective]' })
export class SampleDirective {
...
constructor(..) {
this.customDirective();
}
}
customDirective() {
console.log('Inside customDirective()');
}
Run Code Online (Sandbox Code Playgroud)
我-component.ts
import { SampleDirective } from './my.directive';
...
@Component({
selector: 'my-component',
template: `<button (click)="click()"> Click Me </button>`
})
constructor() { }
..
click() {
// call directive method here
}
Run Code Online (Sandbox Code Playgroud)
我需要这个,因为我正在创建一个通用的解决方案,在运行时借助指令更改组件的DOM结构.
**如果有任何拼写错误,请忽略.抱歉,我无法在此处粘贴完整代码
我试图在角度文档中显示一个类似(不完全)的动态组件.
我有一个带有viewContainerRef的动态指令
@Directive({
selector: '[dynamicComponent]'
})
export class DynamicComponentDirective {
constructor(public viewContainerRef: ViewContainerRef) { }
}
Run Code Online (Sandbox Code Playgroud)
摘自组件代码
@ViewChild(DynamicComponentDirective) adHost: DynamicComponentDirective;
..
ngAfterViewInit() {
let componentFactory = null;
console.log(component);
componentFactory = this.componentFactoryResolver.resolveComponentFactory(component);
// this.adHost.viewContainerRef.clear();
const viewContainerRef = this.adHost.viewContainerRef;
viewContainerRef.createComponent(componentFactory);
}
Run Code Online (Sandbox Code Playgroud)
最后<ng-template dynamicComponent></ng-template>
在模板中添加
每当触发新请求时,我都有一个用例,任何已经在进行中的 http 请求都应该被取消/忽略。
例如:
- 一个请求(比如#2)进来,而请求#1 响应时间太长/网络连接慢。
- 在这种情况下,#2 从服务器获得非常快的响应,然后在任何时候,即使 #1 返回响应,HTTP 响应 observable 也应该被忽略。
- 我面临的问题是,首先组件显示来自请求 #2 的响应值,并在请求 #1 完成时再次更新(这不应该发生)。
我认为 switchMap 取消了 obervables / 维护了发出 observable 值的顺序。
摘自我的 service.ts
Obervable.of('myServiceUrl')
.switchMap(url => this.httpRequest(url) )
.subscribe( response => {
// implementation
/** Update an observable with the
with latest changes from response. this will be
displayed in a component as async */
});
private httpRequest(url) {
return this.httpClient.get('myUrl', { observe: 'response' });
}
Run Code Online (Sandbox Code Playgroud)
上面的实现不起作用。有人能找出这个用例的正确实现吗?
我需要使用时区(短格式)打印时间戳自定义格式:
{{ '2017-06-29 09:55:01.956-0400' | date:'MMM dd, y hh:mm a' }}
Run Code Online (Sandbox Code Playgroud)
输出:
2017 年 6 月 29 日下午 07:25
但我需要附加时区,例如:
2017 年 6 月 29 日美国标准时间下午7:25
角提供时区选项与'z'
和'Z'
,但没有给出预期的结果:
'MMM dd, y hh:mm a z' ==> Jun 29, 2017 07:25 PM India Standard Time
'MMM dd, y hh:mm a Z' ==> Jun 29, 2017 07:25 PM GMT+5:30
Run Code Online (Sandbox Code Playgroud)
我要Jul 7, 2017, 12:27:01 AM IST
。
我在graphql应用程序中工作,无论它是否出现在servlet或服务中,我都必须在json中发送自定义错误对象/消息。
预期的错误响应
{ errorCode: 400 //error goes here,
errorMessage: "my error mesage"}
如果有人可以指导我达到上述要求,将会很有帮助。
angular ×6
observable ×2
rxjs ×2
angular-pipe ×1
date-pipe ×1
datetime ×1
graphql ×1
graphql-java ×1
json ×1
switchmap ×1