Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular: ng-bind-html filters out ng-click?

Tags:

angularjs

I have some html data that I'm loading in from a json file.

I am displaying this html data by using ngSanitize in my app and using ng-bind-html.

Now I would like to convert any links in the json blob from the standard

  • <a href="some_link">link</a>

to:

  • <a ng-click="GotoLink('some_link','_system')">link</a>.

So I'm doing some regExp on the json file to convert the links, but for some reason however ng-bind-html is filtering out the ng-click in it's output, and I can't figure out why. Is it supposed to do this, and if so is it possible to disable this behavior?

Check out this jsFiddle for a demonstration: http://jsfiddle.net/7k8xJ/1/

Any ideas?

like image 618
Squrler Avatar asked Mar 29 '14 22:03

Squrler


People also ask

Is ng-bind HTML safe?

The ng-bind-html directive is a secure way of binding content to an HTML element. When you are letting AngularJS write HTML in your application, you should check the HTML for dangerous code. By including the "angular-sanitize.

Can we use NG-click and Onclick together?

For a single btn, it's ok to use ng-click or onclick in the ng-app . There is no difference between the two functions. For effective team work, you,d better to have an account with each other. In Angular apps, ng-click is recommended.

How does ng-bind HTML work?

The ng-bind directive tells AngularJS to replace the content of an HTML element with the value of a given variable, or expression. If the value of the given variable, or expression, changes, the content of the specified HTML element will be changed as well.

What is the difference between Ng-click and Onclick?

Ans1: The execution context is another key difference between ng-click and onclick. An expression inside of ng-click runs against a specific scope object, often the scope object representing the model for the current controller, whereas code inside an onclick attribute executes against the global window object.


1 Answers

Ok, so the issue is that it isn't compiling the html you include (angular isn't parsing it to find directives and whatnot). Can't think of a way to make it to compile from within the controller, but you could create a directive that includes the content, and compiles it.

So you would change

<p ng-bind-html="name"></p> 

to

<p compile="name"></p> 

And then for the js:

var myApp = angular.module('myApp', ['ngSanitize']); angular.module('myApp') .directive('compile', ['$compile', function ($compile) {   return function(scope, element, attrs) {     scope.$watch(       function(scope) {         return scope.$eval(attrs.compile);       },       function(value) {         element.html(value);         $compile(element.contents())(scope);       }    )};   }]).controller('MyCtrl', function($scope) {     var str = 'hello http://www.cnn.com';     var urlRegEx = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+=&;%@\.\w]*)#?(?:[\.\!\/\\\w]*))?)/g;     result = str.replace(urlRegEx, "<a ng-click=\"GotoLink('$1',\'_system\')\">$1</a>");     $scope.GotoLink = function() { alert(); }     $scope.name = result; }); 

Angular 1.2.12: http://jsfiddle.net/7k8xJ/4/

Angular 1.4.3: http://jsfiddle.net/5g6z58yy/ (same code as before, but some people were saying it doesn't work on 1.4.*)

like image 68
dave Avatar answered Sep 22 '22 07:09

dave