0

I am trying to parse html value in my angular page, not sure what I am doing wrong, I am getting this error in my console:

app.js:13 Uncaught SyntaxError: Unexpected token )

app.js

var App = angular.module('App', []);

App.controller('ProjectCtrl', function($scope, $http) {
  $http.get('app/test.json')
       .then(function(res){
          $scope.projects = res.data;
        });

       App.filter('to_trusted', ['$sce', function($sce){
    return function(text) {
        return $sce.trustAsHtml(text);
    };
});

test.json

[
  { "icon": "<i class="fa fa-github fa-3x" aria-hidden="true"></i>", "name": "lovelycss" },
  { "icon": "<i class="fa fa-github fa-3x" aria-hidden="true"></i>", "name": "lovely-icons" }
]

html:

<div ng-controller="ProjectCtrl">
  <ul>
    <li ng-repeat="project in projects">
     <div ng-bind-html="'{{project.icon}}' | to_trusted"></div>- <em>{{project.name}}</em>
    </li>
  </ul>
</div>

what I am trying to archive is this: http://jsfiddle.net/JfGVE/1547/

I want a json loading the icons and the text.

I hope now this will be clear.

raduken
  • 2,091
  • 16
  • 67
  • 105

2 Answers2

4

You are missing a } in your controller declaration and a [ in your filter declaration:

var App = angular.module('App', []);

App.controller('ProjectCtrl', function($scope, $http) {
    $http.get('app/test.json')
         .then(function(res){
            $scope.projects = res.data;
          });
}); // You are missing a '}' here

App.filter('to_trusted', ['$sce', function($sce){
    return function(text) {
        return $sce.trustAsHtml(text);
    };
}]); // You are missing a ']' here

You will also have to edit your JSON to escape your quotes "

[
    {
        "icon": "<i class=\"fa fa-github fa-3x\" aria-hidden=\"true\"></i>",
        "name": "lovelycss"
    }, {
        "icon": "<i class=\"fa fa-github fa-3x\" aria-hidden=\"true\"></i>",
        "name": "lovely-icons"
    }
]

And the expression you are passing to ng-bind-html is also wrong. Because of the single quotes ' you are passing a literal string '{{project.icon}}' to the filter. You have to remove the quotes and the curly braces, because the ng-bind-html directive needs an expression as a parameter.

<div ng-controller="ProjectCtrl">
  <ul>
    <li ng-repeat="project in projects">
     <div ng-bind-html="project.icon | to_trusted"></div>- <em>{{project.name}}</em>
    </li>
  </ul>
</div>
Alvaro Vazquez
  • 372
  • 3
  • 9
  • thanks, but i am getting this error now: SyntaxError: Unexpected token f in JSON at position 25 at xc (angular.js:1326) at dc (angular.js:10515) at angular.js:10606 at r (angular.js:321) at hd (angular.js:10605) at c (angular.js:11403) at angular.js:16170 at m.$eval (angular.js:17444) at m.$digest (angular.js:17257) at m.scopePrototype.$digest (hint.js:1364) – raduken Sep 09 '16 at 10:28
  • @Raduken That's because you have to scape the quotes (") with a backslash this way: \" I will edit my answer to add that. – Alvaro Vazquez Sep 09 '16 at 10:31
  • thanks but in my html is loading this: {{project.icon}} , to be fair I want a Icon there rendered, understand? I don't want show the tag but the icon, understand? – raduken Sep 09 '16 at 10:44
  • @Raduken I understand. Please, edit your question providing all the information, with all the steps you have gone through. I will edit my answer to provide a solution for that problem as well. – Alvaro Vazquez Sep 09 '16 at 10:48
  • @Raduken Answer editted. I hope this is the final solution :) – Alvaro Vazquez Sep 09 '16 at 10:54
3

The message tells you the problem: you have a syntax error. Two actually.

  1. the function for your controller is not closed.
  2. there is not closing bracket for the filter.

app.js

var App = angular.module('App', []);

App.controller('ProjectCtrl', function($scope, $http) {
  $http.get('app/test.json')
       .then(function(res){
          $scope.projects = res.data;
        });
}); // 1

App.filter('to_trusted', ['$sce', function($sce){
    return function(text) {
        return $sce.trustAsHtml(text);
    };
}]); // 2

For JSON ERROR fix using this

[
  { "icon": "<i class='fa fa-github fa-3x' aria-hidden='true'></i>", "name": "lovelycss" },
  { "icon": "<i class='fa fa-github fa-3x' aria-hidden='true'></i>", "name": "lovely-icons" }
]
cjmling
  • 6,896
  • 10
  • 43
  • 79
kjaquier
  • 824
  • 4
  • 11
  • thanks but I am getting this error now: SyntaxError: Unexpected token f in JSON at position 25 at xc (angular.js:1326) at dc (angular.js:10515) at angular.js:10606 at r (angular.js:321) at hd (angular.js:10605) at c (angular.js:11403) at angular.js:16170 at m.$eval (angular.js:17444) at m.$digest (angular.js:17257) at m.scopePrototype.$digest (hint.js:1364) – raduken Sep 09 '16 at 10:28
  • You have used double quotes inside the string content in the json (which is not allowed since we're already using double quotes for strings) ; see the edit – kjaquier Sep 09 '16 at 10:39
  • thanks but in my html is loading this: {{project.icon}} , to be fair I want a Icon there rendered, understand? I don't want show the tag but the icon, understand? – raduken Sep 09 '16 at 10:44