1.{{}}
花括号里绑定数据时,是定义一个全局变量放里面比较好,还是定义一个return
的函数比较好?比如:
<p>{{time}}</p>
export class XXX1{
private time;
constructor(){...}
getTime(){
setInterval(()=>{
this.time=new Date().getTime()+500;
},500);
}
}
<p>{{getTime()}}</p>
export class XXX2{
constructor(){...}
getTime(){
var time;
setInterval(()=>{
time=new Date().getTime()+500;
},500);
return time;
}
}
2.我的项目遇到一个问题,跳路由的时候,执行了构造函数construction()
,但并没有立即执行ngOnInit
方法,而是过了几秒以后或者页面有变化的时候才执行。
我看到官网有这样一句话:
Remember also that a directive's data-bound input properties are not set until after construction. That's a problem if we need to initialize the directive based on those properties. They'll have been set when our ngOninit runs.
中文官网是这样翻译的:
另外还要记住,在指令的 构造函数完成之前 ,那些被绑定的输入属性还都没有值。 如果我们需要基于这些属性的值来初始化这个指令,这种情况就会出问题。 而当 ngOnInit 执行的时候,这些属性都已经被正确的赋值过了。
这句话没搞懂?不知道是不是因为这个原因我的路由没有立即执行ngOnInit
方法?
回答一下第一个问题:
我还是习惯用property吧,毕竟以后如果是双向绑定的话,在property的setter里面触发绑定的event比较自然吧。