Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
136
rated 0 times [  143] [ 7]  / answers: 1 / hits: 5609  / 11 Years ago, mon, january 27, 2014, 12:00:00

Hi im new to angular JS and just getting to grips with it, Im trying to create a filter that will compile the given string that is stored in the database. The HTML is below which passes history.activity to my dynamic filter with :this appended to pass in the current scope.



<ul>
<li ng-repeat=history in recentActivity>
{{ history.activity | dynamic:this }}</p>
</li>
</ul>


The history.activity variable contains a string in the format of an Angular template like below, these will vary as they are pulled in from the database based on the logged activity.



{{ history.username }} logged in at {{ history.created_date | date }}


My filter below is extremely basic and should compile the source but its just returning undefined.....



app.filter('dynamic', function ($compile) {
return function (source, scope) {
return $compile(source)(scope);
};
});


Any ideas? where am i going wrong with this? any help is greatly appreciated :)


More From » angularjs

 Answers
3

If your activity variable does not contain HTML but just angular expression then you need to use $interpolate instead of $compile - check the angular.js docs for it.



If you need to put some HTML dynamically then you need the directive as Jonathan said - filters cannot be used to include HTML.



But actually I'd like to suggest using directive anyway - both compilation/interpolation are relatively slow so it would be better to call them only when your expression is changed (which could be achived with $watch call inside directive). Filter expressions are called twice each digest cycle and you may meet performance problems if you'll use this filter often.


[#48299] Monday, January 27, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
micayla

Total Points: 148
Total Questions: 92
Total Answers: 109

Location: Aruba
Member since Sat, Oct 2, 2021
3 Years ago
micayla questions
Fri, Dec 24, 21, 00:00, 2 Years ago
Thu, Apr 16, 20, 00:00, 4 Years ago
Thu, Nov 14, 19, 00:00, 5 Years ago
;