Angularjs承诺检查自定义元素的日期

问题描述 投票:-1回答:2

我是angularjs编程的新手。我正在开发一个项目,我需要执行我的服务,一旦完成,我就可以通过它获取数据。对不起,我无法分享确切的代码,因为它是与工作相关的项目。我的服务名称是applicationservice里面我有一个方法调用get allapplications我在执行它后得到我的数据数组。我需要检查该数据数组的name属性是否存在于用户类型的应用程序名称之外。我是怎么克服这个的?

this.applicationsService.getAllApplications().then((data) => {
  if (isFound === true) {
    this.handleError('Sorry, This application already exists'); } })
      .catch((error) => {

      });
javascript angularjs
2个回答
0
投票

基本上你缺少的是通过迭代数组来检查你的数据内容。你需要的是一个循环来完成这些数据。为此你可以尝试这样的事情。

 this.applicationsService.getAllApplications().then((data) => {
          let isFound = false;
          for (let i = 0; i < data.length; i++) {
            if (data[i].name.toLowerCase() === /*usergivenApplication.toLowerCase()*/) {
              isFound = true;
              break;
            }
          }
      if (isFound === true) {
        this.handleError('Sorry, This application already exists'); } })
          .catch((error) => {

          });

0
投票

在不知道从服务返回的数据结构是什么的情况下,您的代码应该类似于:

let  user_typed_app_name_defined_before = "someAppName";

this.applicationsService.getAllApplications().then((data) => {
    var apps = data.applications;
    var isFound = false 
    for (var i=0;i<apps.length;i++){
        let app = apps[i]
        if(app["name"] == user_typed_app_name_defined_before){
            isFound = true
        }
    }  
    if (isFound === true) {
        this.handleError('Sorry, This application already exists'); 
    }else{
        // do your logic
    }
})
.catch((error) => {

});
© www.soinside.com 2019 - 2024. All rights reserved.