0

I am using AngularJS to load AJAX content and using ng-repeat to create a list of items. On the content I also have {{noun}} as a placeholder. My assumption was that when the ajax content is loaded, AngularJS would automatically replace {{noun}} with the data from $scope.noun model. But it isn't the case. Any quick and dirty way to make it happen?

Here's my code:

AllControllers.controller('AppController', ['$scope', '$http', function ($scope, $http) {

    $scope.noun = "My Noun";

    $scope.headlines = [{
        headline: "Top 10 Tricks to {{noun}}",
        usage: 10,
        votes: 100
    }];

    $scope.load_headlines = function() {
        $http.get('/data/headlines.json').
            success(function(data, status, headers, config){
                $scope.headlines = data;
            }).
            error(function(data, status, headers, config){
                console.log(status);
            });
    };
    }]);

<div ng-controller="AppController" ng-init="load_headlines()">

    <table>
    <tbody ng-repeat="headline in headlines">
    <tr>
        <td>{{headline.headline}}</td>
        <td class="center">{{headline.usage}}</td>
        <td class="center">{{headline.votes}}</td>
    </tr>
    </tbody>
    </table>

</div>
7
  • Where is the Ajax code you are talking about? Commented May 20, 2015 at 6:51
  • I didn't think it was necessary to include the ajax call. Because at the end of the day the $scope.headlines will be an array of objects. And the attribute headline will have the placeholder {{noun}}. So, how you populate $scope.headlines may not be relevant. Commented May 20, 2015 at 6:56
  • Where is headlines_displayed coming from? Commented May 20, 2015 at 6:59
  • Does it work when you omit the load_headlines method and just use the fixed version of $scope.headlines? Commented May 20, 2015 at 7:00
  • Good catch. That was a typo, should be headlines. Still, no change to {{noun}} when the headline attribute is rendered. Commented May 20, 2015 at 7:00

4 Answers 4

1

Your $scope variable is differently to the ngRepeat variable. I think you have to change the $scope variable in your controller:

$scope.headlines_displayed = [{
        headline: "Top 10 Tricks to "+$scope.noun,
        usage: 10,
        votes: 100
    }];
Sign up to request clarification or add additional context in comments.

Comments

1

you can binding the {{moon}} in the td repeated.

change the code like this:

AllControllers.controller('AppController', ['$scope', '$http', function ($scope, $http) {

    $scope.noun = "My Noun";

    $scope.headlines = [{
        headline: "Top 10 Tricks to ",
        usage: 10,
        votes: 100
    }];
}]);

<tbody ng-repeat="headline in headlines_displayed">
<tr>
    <td>{{headline.headline}} {{noun}}</td>
    <td class="center">{{headline.usage}}</td>
    <td class="center">{{headline.votes}}</td>
</tr>
</tbody>

2 Comments

That won't work. Because I'm populating $scope.headlines using an ajax call. The $scope.headlines is dynamic.
i update my answer, you can binding tow params in html;
0

Figured it out. Ended up doing this:

<td>{{headline.headline.replace("{{noun\}\}", noun)}}</td>

Comments

0

You should be using $interpolate service to compile the string before assigning it to the scope. Passing a string with {{}} will not work. In your http success callback do something like this. This will replace your {{noun}} with scope value.

 $scope.load_headlines = function() {
    $http.get('/data/headlines.json').
        success(function(data, status, headers, config){
            $scope.headlines = data;
            $interpolate($scope.headlines.headline)($scope);
        }).
        error(function(data, status, headers, config){
            console.log(status);
        });
};
}]);

Take a look at the the this fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.