-1

I am facing an issue with angularJS, basically I trying to print the "img" child of below, but it does not print anything and does not even show an error. I have followed many tutorials of angularJS, but I am stuck at this point. Any ideas on what's can be wrong ?

Here is the angularJS code :

    app.controller("Deals", function($scope){
    $scope.getDeal = function() {
    [
    {
        "name":"Joe"
        "img":"https://i.ytimg.com/vi/NsvH9ye1ycU/mqdefault.jpg"

    }];}    
});

Calling from HTML :

<ion-content ng-controller="Deals">
<h6>{{getDeal.img}}</h6>
</ion-content>

The result is blank; however I am willing to get the url of that image.

Zikooz
  • 175
  • 1
  • 2
  • 9

3 Answers3

0

The problem should be cause of square brackets. which means it needs ng-repeat. try this it should work

<ion-content ng-controller="Deals">
<h6 ng-repeat="deal in getDeal">{{deal.img}}</h6>
</ion-content>

also remove quotes from keys of object. like this:

$scope.getDeal = 
    [{
        name: "Joe",
        img: "https://i.ytimg.com/vi/NsvH9ye1ycU/mqdefault.jpg"

    }];

check codepen

Arif
  • 1,617
  • 9
  • 18
0

That is an Array which you are using, so when you use an Array you must use the array Index as Well. So the correct Code Will Be

`<ion-content ng-controller="Deals">
<h6>{{getDeal[0].img}}</h6>
</ion-content>`
0

Ok, when you're repeating, you need to call the getDeal() function in the ng-repeat. Here's an example,

https://jsbin.com/tavapuyaki/edit?html,js,output

<div ng-repeat="deal in getDeal()">
  {{deal.name}} <img ng-src="deal.img"/>
</div>

Also, your function is not returning anything. You need something like this:

$scope.getDeal = function() {
return [
{
    "name":"Joe",
    "img":"https://i.ytimg.com/vi/NsvH9ye1ycU/mqdefault.jpg"

}];

};

However, I would advise against doing that and creating a property that stores the contents of the array.

For instance,

$scope.deals = [{name:'myname', img: 'blah.jpg'}];

Here is another SO answer explaining as to why using a function is not advised in an ng-repeat. How to Loop through items returned by a function with ng-repeat?

Community
  • 1
  • 1
John F.
  • 4,780
  • 5
  • 28
  • 40