brucelee
brucelee

Reputation: 147

adding angular code within html

in Angular this would be allowed:

{{ 5 + 5 }}

but something like this cannot

{{ angular.forEach(something, function(item){
 console.log(item._id);
}); }}

But I really want such a thing to work, because I want to combine ng-repeat while validating certain values. If I validate it with code in the controller, it's doesn't keep track of new changes. It doesn't bind real time, I would need the page to refresh.

Is my question making sense?

More info for perhaps a different approach: if I add a item to something, it won't console.log the item id if my forEach code is within the controller. I would need to refresh the page. my something is full of data that comes from my database, so in the view any data added to it won't be shown unless I refresh the page. Simply showing the data with {{ }} will show it without having to refresh, but it seems that my code is limited if I use {{ }}. How do I work this out?

Upvotes: 1

Views: 48

Answers (1)

Miguel
Miguel

Reputation: 20633

in controller, bind function to scope property:

$scope.doSomething = function(something) {
    return something.toUpperCase();
};

in template call the scoped function:

<li ng-repeat="something in things">
    {{ doSomething(something) }}
</li>

Upvotes: 3

Related Questions