0

I am building an Angular 1.5 app using the component structure. After the promise comes back from the $http call in the service, I am trying to call another function to filter the dataset before it is displayed on the UI.

However, the filterApps function is not getting called.

Also...in the filterApps function I am trying to compare to arrays of objects and return back the ones that have the same name. Is this the best way to go about this or is there a cleaner way?

Controller :

import allApps from '../../resources/data/application_data.js';

class HomeController {
    /*@ngInject*/
    constructor(ItemsService) {
        this.itemsService = ItemsService;
        this.displayApps = [];
    }

    $onInit() {
        this.itemsService
            .getItems()
            .success((apps) => this.filterApps(apps));
    }

    filterApps(siteApps) {
        this.displayApps = allApps.applications.filter((app) => {
            siteApps.applications.map((siteApp) => {
                if(siteApp.name === app.name) {
                    return app;
                }
            })
        });
    }
}

export default HomeController;
8
  • I don't see any $http call... Commented Aug 12, 2016 at 20:00
  • 2
    Does it really get into success callback, try adding .error((error) => console.log(error))? Are you sure getItems does return a promise.. check console as well for error Commented Aug 12, 2016 at 20:00
  • @DannyBuonocore the $http call is in a service which has nothing to do with the question Commented Aug 12, 2016 at 20:01
  • @PankajParkar yes it does go into the success callback. Previously I had this.itemsService .getItems() .success((apps) => this.displayApps = apps); and all the apps were displayed Commented Aug 12, 2016 at 20:02
  • 1
    You'd be surprised how many questions on here can't be answered because the the OP thought the offending code "had nothing to do with the question"... just sayin' Commented Aug 12, 2016 at 20:03

1 Answer 1

1

I don't see any reason that filterApps method isn't geting call(as you already commented that success function is getting called). I guess you're just checking nothing has been carried in displayApps variable. The real problem is you have not return internal map function result to filter. So that's why nothing gets return.

Code

filterApps(siteApps) {
    this.displayApps = allApps.applications.filter((app) => {
        //returning map function result.
        return siteApps.applications.map((siteApp) => {
            if(siteApp.name === app.name) {
                return app;
            }
        })
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I was missing that return. Also is there a better way to do that array comparison?
@erichardson30 it seems correct to me.. as you are comparing name of an object for equality.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.