我想通过使用setInterval()每隔10分钟调用一个函数,并且在这个函数中我想使用从Angular 2的Dependency Injector获得的Service(称为auth),问题是控制台告诉我以下内容:
EXCEPTION:TypeError:this.auth未定义
constructor(private auth: AuthService){
setInterval(function(){ this.auth.refreshToken(); }, 1000 * 60 * 10);
}
Run Code Online (Sandbox Code Playgroud) 我搜索了所有可能的方法,但我无法弄清楚如何组合返回类型注释和胖箭头语法.
class BasicCalculator{
value:number;
constructor(value:number=0){
this.value=value;
}
add= (operand:number)=>{ // CAVEAT how to add return type???
this.value+=operand;
return this;
}
subtract= (operand:number)=>{
this.value-=operand;
return this;
}
multiply= (operand:number)=>{
this.value*=operand;
return this;
}
divide= (operand:number)=>{
this.value/=operand;
return this;
}
}
Run Code Online (Sandbox Code Playgroud)
我试过了:
add= (operand:number)=>number { ....
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
如果在同一元素上有过滤器的ng-if和ng-repeat,则即使ng-if隐藏了元素,也会调用过滤器一次.
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, $timeout) {
$timeout(function() {
$scope.list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
}, 2000);
});
app.filter('crashyFilter', function() {
return function(list) {
return list.map(function(item) {
return item + 1;
});
};
});
Run Code Online (Sandbox Code Playgroud)
<body ng-controller="MainCtrl">
<div ng-if="list" ng-repeat="item in list | crashyFilter">
{{item}}
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
在浏览器控制台中,您可以看到地图调用失败一次,因为list参数未定义.我在这里做了一个龙头.
谁知道为什么会这样?