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.

2

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

5 Comments

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 That's because you have to scape the quotes (") with a backslash this way: \" I will edit my answer to add that.
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 <i> tag but the icon, understand?
@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.
@Raduken Answer editted. I hope this is the final solution :)
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" }
]

3 Comments

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)
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
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 <i> tag but the icon, understand?

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.