0

I'm new with Angular, and i am making an application with ionic. I can't show HTML from JSON in my view. I searched previous questions but still doesn't work. The html code is written as text..

My code

HTML

<div ng-bind-html="bindHTML"></div>

Json

"usuarios": [
    {
      "nombre": "Name 1",
      "description":"<p>Some HTML</p><p>More HTML</p>",
      "id": 0
    },
    {
      "nombre": "Name 2",
      "description":"<p>Some HTML</p><p>More HTML</p>",
      "id": 1
    }
]

Controller

.controller('UserCtrl', ['$scope', '$http', '$state', function($scope, $http, $state) {
    $http.get('js/data.json')
        .success(function(data){
            $scope.data = data.usuarios[$state.params.id];
            $scope.bindHTML = $sce.trustAsHtml(data.description);
        });
}])

Thanks.

10
  • Duplicate of stackoverflow.com/questions/31333151/… Commented May 12, 2016 at 1:23
  • 1
    As I say in my question, I tried this code and it doesn'work =( Commented May 12, 2016 at 1:25
  • Then why isn't showing in your controller or your markup? Commented May 12, 2016 at 1:26
  • I don't know, I'm trying for the last 3 hours with different codes and nothing Commented May 12, 2016 at 1:28
  • implement the preprocessor in your code and show your work and I'll tell you what you're doing wrong. Commented May 12, 2016 at 1:30

1 Answer 1

3

I implemented a plunker to illustrate how this works. The only thing I didn't include is the ui.router $state stuff you are doing.

Controller

app.controller('MainCtrl', function($scope, $http, $sce) {
    $http.get('data.json').success(function(data){
        $scope.data = $sce.trustAsHtml(data.usuarios[0].descripcion);
    });
});

View

 <body ng-app="plunker" ng-controller="MainCtrl">
    <p ng-bind-html="data"></p>
  </body>

I'm not sure why yours wouldn't be working if you tried what was suggested in the thread that was marked as duplicate. That would leave me to believe it has something to do with the $state dependency, but hard to tell with out seeing your full app.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your answer. I tried that but it didn't work. I put in this plunker plunker.co/edit/Hd2laRauUq all my code. I have a list in comunidad.html, when i click in a row i go to user.html where i show the data.description (json from each user)
Your <script> tags weren't pointing to the right stuff in the plunker... I changed them and stuff showed up in the preview... <!-- ionic/angularjs js --> <script src="https://cdnjs.cloudflare.com/ajax/libs/ionic/1.3.0/js/ionic.bundle.js"></script> <!-- your app's js --> <script src="lib/script.js"></script>
Oh Sorry, i fix the url. Please, click in "Comunidad" tab. And then click in an item list. There in user.html You can see the name, but the description is not seen.
your user controller was missing the "$sce" dependency being passed in, and it was referencing "data" not "$scope.data"... .controller('UserCtrl', ['$scope', '$http', '$state', '$sce', function($scope, $http, $state, $sce) { $http.get('js/data.json').success(function(data){ $scope.data = data.usuarios[$state.params.id]; $scope.bindHTML = $sce.trustAsHtml($scope.data.description); }); }])
Now it works!!! Thank you very much!! I'm new and I still don't understand much. Thanks for your time!. If I can do something for you just write me.
|

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.