Javascript缩短和浓缩函数
我有一个函数,我想缩短它,使它更简单,因为我不擅长使用javascript,当我试图缩短它时,我总是会出错:Javascript缩短和浓缩函数,javascript,angularjs,Javascript,Angularjs,我有一个函数,我想缩短它,使它更简单,因为我不擅长使用javascript,当我试图缩短它时,我总是会出错: $scope.doRefresh = function (){ if($scope.bulletpointPopular){ ArticleService.popular().then(function(data){ $scope.articles = data; }) .finally(function() {
$scope.doRefresh = function (){
if($scope.bulletpointPopular){
ArticleService.popular().then(function(data){
$scope.articles = data;
})
.finally(function() {
$scope.$broadcast('scroll.refreshComplete');
});
}
else {
ArticleService.all().then(function(data){
$scope.articles = data;
})
.finally(function() {
$scope.$broadcast('scroll.refreshComplete');
});
}
};
为此:
$scope.doRefresh = function (){
if($scope.bulletpointPopular){
$scope.popular();
}
else {
$scope.latest();
}
.finally(function() {
$scope.$broadcast('scroll.refreshComplete');
});
};
Erorr:
未捕获的语法错误:意外标记
怎么样。因此,我看到的唯一区别是if中的逻辑和elseif中调用ArticleService的函数之间的区别。因此,将其作为一个变量,并通过从ArticleService作为属性访问它来调用它
或
$scope.doRefresh = function (){
var articlePromise = $scope.bulletpointPopular? ArticleService.popular(): ArticleService.all();
articlePromise.then(function(data){
$scope.articles = data;
}).finally(function() {
$scope.$broadcast('scroll.refreshComplete');
});
};
在本例中,基于布尔值,调用相应的函数并获取返回的承诺,然后解析
怎么样。因此,我看到的唯一区别是if中的逻辑和elseif中调用ArticleService的函数之间的区别。因此,将其作为一个变量,并通过从ArticleService作为属性访问它来调用它
或
$scope.doRefresh = function (){
var articlePromise = $scope.bulletpointPopular? ArticleService.popular(): ArticleService.all();
articlePromise.then(function(data){
$scope.articles = data;
}).finally(function() {
$scope.$broadcast('scroll.refreshComplete');
});
};
在这种情况下,根据布尔值,调用相应的函数并获得返回的承诺,然后进行解析。不太确定代码的逻辑,但可以在ArticleService中使用输入参数bulletpointPopular创建一个新方法,该方法将调用popular()或all()根据bulletpointPopular的值,在本例中,您的代码将更短,如下所示
$scope.doRefresh = function (){
ArticleService.newMethod($scope.bulletpointPopular).then(function(data){
$scope.articles = data;
})
.finally(function() {
$scope.$broadcast('scroll.refreshComplete');
});
};
不太确定代码的逻辑,但可以在ArticleService中使用输入参数bulletpointPopular创建一个新方法,该方法将根据bulletpointPopular值调用popular()或all(),在这种情况下,代码将更短,如下所示
$scope.doRefresh = function (){
ArticleService.newMethod($scope.bulletpointPopular).then(function(data){
$scope.articles = data;
})
.finally(function() {
$scope.$broadcast('scroll.refreshComplete');
});
};
您可以这样做:
$scope.popular = function() {
return ArticleService.popular();
};
$scope.latest = function() {
return ArticleService.all();
};
$scope.doRefresh = function() {
($scope.bulletpointPopular ? $scope.popular() : $scope.latest()).then(function(data) {
$scope.articles = data;
}).finally(function() {
$scope.$broadcast('scroll.refreshComplete');
});
};
您可以这样做:
$scope.popular = function() {
return ArticleService.popular();
};
$scope.latest = function() {
return ArticleService.all();
};
$scope.doRefresh = function() {
($scope.bulletpointPopular ? $scope.popular() : $scope.latest()).then(function(data) {
$scope.articles = data;
}).finally(function() {
$scope.$broadcast('scroll.refreshComplete');
});
};
你会犯什么错误?你忘了提到你遇到了什么错误?你忘了mention@Chanthu答案似乎比较好,不知道服务方法可以调用吗that@Chanthu答案似乎更好,不知道服务方法可以这样调用