2

I have a custom filter that I use to return an html string using $sce.trustAsHtml. In the template/view I use ng-bind-html directive and pass the filter to as follows:

<div ng-bind-html="userAgent | geoCode:business"></div>

Inside my filter I have an inner function that takes an input, business model in my case, which takes properties from the business model, a mongoosejs model, and generates a formatted string which is used to generate and html a tag:

'<a href="some_url_i_create" ...>'+ myFormattingInnerFunction(business) +'</a>';

What is strange is, if I use this function several of the fields are returned as undefined/blank. However, if I directly access the variables a follows:

'<a href="some_url_i_create" ...>'+ business.prop1 + business.prop2+ ... +'</a>';

Then all of the properties are found and output. Any ideas?

P.S. The model is a retrieved via an AJAX request, which in turn use mongoosejs to retrieve the data, inside of the angular controller for this section.

2
  • Where do you store that business variable? How does the filter aquire the reference to it? Commented Apr 9, 2015 at 14:25
  • It is requested in the controller and is appended to the $scope variable in a callback. Commented Apr 9, 2015 at 14:27

1 Answer 1

3

When you are calling the function you are accessing the business object on the first rendering event when all the scripts are loaded and the first DOM manipulation occurs.

But When you accessing the business object directly angular puts the values into the view on the first rendering event and on all of the digest events and of course on other rendering events.

You might want to consider using a $scope. attr in the template to render and call the formated string value method in the callback as well.

In the JS:

.success(function(data){
   business=JSON.parse(data);//Iguess
   $scope.formatedtext= myFormattingInnerFunction(business);
   $scope.$apply();// If you use an angular libs callback this probably not needed.
})

In the template:

'<a href="some_url_i_create" ...>'+ formatedtext +'</a>';
Sign up to request clarification or add additional context in comments.

2 Comments

I'll give that a shot and see what happens. Also, I've heard about $digest, and had logging in my filter which showed that it executed 4 or 5 times. Can you describe digest more or give a link to documentation about it?
Here is a pretty good explanation: sitepoint.com/understanding-angulars-apply-digest

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.