0

In my module I have a service that calls $http and gets an Array of objects,

function MenuSearchService($http){       
        this.getMatchedMenuItems=function(searchItem){
             var foundItems =$http({
                method:'GET',
                url:'https://whateverurl.com/whateverdata'
            });
            return foundItems;
        }; // end function
    }; // end service

and I use a controller (as a syntax) to process the data,

function MenuController(MenuSearchService){
        this.query;
        this.results;
        this.promise;
        this.searchClicked=false;
        this.clickToSearch = function(searchItem){
            this.searchClicked=true;
            this.query=searchItem;
            this.promise=MenuSearchService.getMatchedMenuItems()
            .then(function(result){
                this.foundItems = result.data;
                console.log(this.foundItems);        
                }); 
        }; // end onclick
    }; // end controller

I am able to retrieve the data. Here is the stdout of foundItems:

enter image description here

Each object in the foundItems has properties name and description.But when I want to present it in the view,

<section class="container" ng-controller="MenuController as m">
    <table>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Description</th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="item in m.foundItems">               
                        <td>{{item.name}}</td>
                        <td>{{item.description}}</td>
                    </tr>
                </tbody>
            </table>
</section>

the data is not shown. Can anyone tell what goes wrong?


Update After taking a look at @charlietfl's comment I updated my code to:

function MenuController(MenuSearchService){
        var m=this;
        m.query;
        m.promise;
        m.searchClicked=false;
        m.clickToSearch = function(searchItem){
            m.searchClicked=true;
            m.query=searchItem;
            m.promise=MenuSearchService.getMatchedMenuItems()
                .then(function(result){
                        m.foundItems = result.data;                       
                        console.log(m.foundItems);  

                    }).catch(function (error) {
                        console.log(error.message());
                });
            }; // end onclick
        }; // end controller

The concept is wrongly using this. in a function inside a controller.

Dylan Czenski
  • 1,305
  • 4
  • 29
  • 49
  • 1
    `this` inside `then()` is not what you think it is. Store a captured reference of `this` once...then only use that variable from then on See [John Papa Angular style guide](https://github.com/johnpapa/angular-styleguide/tree/master/a1#style-y032) – charlietfl Dec 31 '16 at 20:51
  • 1
    IMO, this shouldn't have been closed as a duplicate for the reason it was. There are really two problems here. The first is what charlietfl referenced. The second is the fact that you are treating 'foundItems' as an array, when it is actually an object (result.data is an object). When you console.log foundItems, you see that it is an object that has an array of menu_items in it. So switch your ng-repeat to "item in m.foundItems.menu_items". Or better yet, set foundItems = result.data.menu_items – Chris Newman Dec 31 '16 at 22:47
  • @charlietfl should I delete this question? – Dylan Czenski Dec 31 '16 at 23:10
  • you can...it will likely get system deleted at some time. Did the link help? – charlietfl Dec 31 '16 at 23:40

0 Answers0