返回函数中的布尔值Promise,用于CanActivate函数[Angular Guard]

时间:2018-03-07 17:49:02

标签: angularjs angular-promise guard canactivate

我想从这个函数返回一个布尔承诺,请帮忙吗?

 getSectionsList() {
let sections: string[] = [];
var ref = this.db.database.ref(`instructors/jDlPCWMMJ8YmD9tMO3shTshM5sx1/sections`);
return ref.once('value').then(function (snap) {
  var array = snap.val();
  for (var i in array) {
    var value = array[i];
    sections.push(value.section)
    //console.log(value.section)
  }
  if (sections.includes('SPRING_2018_CMPS333_L01'))
    return (true)
  else
    return (false)

}).catch(function (err) {
  console.log(err)
})

}

1 个答案:

答案 0 :(得分:0)

尝试注入AngularJS $ q服务......

if (sections.includes('SPRING_2018_CMPS333_L01'))
  return $q.resolve(true);
else
  return $q.resolve(false);

你也可以$ q.reject(false)