AngularJS: How to render HTML in $scope variable in view?

Soumik Rakshit

I have been trying to create a blog app using the MEAN stack. I am having problem to render the posts as HTML. On the browser, it is just showing {{posts}}

HTML code:

<!DOCTYPE html>
<html lang="en" ng-app="BlogApp">
    <head>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
        <script src="app.js"></script>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
        <div class="container" ng-controller="BlogController">
            <h1>Blog</h1>
            <input ng-model="post.title" class="form-control" placeholder="title"/>
            <textarea ng-model="post.body" class="form-control" placeholder="body"></textarea>
            <button ng-click="createPost(post)" class="btn btn-primary btn-block">Post</button>

            {{posts}}

        </div>
    </body>
</html>

Angular code:

(function() {
    angular.module("BlogApp", []).controller("BlogController", BlogController);
    function BlogController($scope, $http) {
        $scope.createPost=createPost;

        function init() {
            getAllPosts();
        }

        init();

        function getAllPosts() {
            $http.get("/api/blogpost").success(function(posts) {
                $scope.posts=posts;
            });
        }

        function createPost(post) {
            console.log(post);
            $http.post("/api/blogpost", post);
        }
    }
})();
Vivz

Since you are using angular 1.6.x, you have to use .then and .catch instead of .success and .error because the latter are deprecated in newer versions.

  (function() {
        angular.module("BlogApp", []).controller("BlogController", BlogController);
        function BlogController($scope, $http) {
            $scope.createPost=createPost;

            getAllPosts();

        function getAllPosts() {
                $http.get("/api/blogpost").then(function(response) {
                    $scope.posts=response.data; //check if this is what you are looking for
                });
            }

            function createPost(post) {
                console.log(post);
                $http.post("/api/blogpost", post);
            }
        }
    })();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Render angular's html from the variable on scope

From Dev

Render angular's html from the variable on scope

From Dev

Angularjs: $scope.variable not in sync with view?

From Dev

AngularJS $scope variable does not go into view

From Dev

AngularJS update controller variable and render the view again?

From Dev

How to render variable to view in slim?

From Dev

Push HTML to the view with variable AngularJS

From Dev

How to render raw html with AngularJS?

From Dev

How to render text into HTML in AngularJS

From Dev

How to render html in Angularjs directive?

From Dev

How to render raw html with AngularJS?

From Dev

AngularJS + Json: How to render html

From Dev

Angular JS render variable in scope function to view and make comparison

From Dev

How to bind a $scope variable to a normal variable in AngularJS?

From Dev

Render Razor variable inside AngularJS Html

From Dev

Render Razor variable inside AngularJS Html

From Dev

AngularJS: How to bind a $scope variable to another $scope.variable

From Dev

How to set default value for scope variable in angularjs

From Dev

AngularJS how to dynamically update part of scope with variable

From Dev

How to populate Angularjs $scope variable in javascript?

From Dev

How to check if a scope variable is undefined in AngularJS template?

From Dev

How to track scope variable changing in AngularJS

From Dev

How to asynchronously populate a $scope variable in AngularJS?

From Dev

How to set scope variable value in angularjs template

From Dev

How to set scope variable value in angularjs template

From Dev

How to update $scope variable value in AngularJs

From Dev

How to bind a $scope variable with text editor in angularjs

From Dev

how to store Specific Column in scope variable Angularjs

From Dev

how to access angularjs scope variable in javascript

Related Related

  1. 1

    Render angular's html from the variable on scope

  2. 2

    Render angular's html from the variable on scope

  3. 3

    Angularjs: $scope.variable not in sync with view?

  4. 4

    AngularJS $scope variable does not go into view

  5. 5

    AngularJS update controller variable and render the view again?

  6. 6

    How to render variable to view in slim?

  7. 7

    Push HTML to the view with variable AngularJS

  8. 8

    How to render raw html with AngularJS?

  9. 9

    How to render text into HTML in AngularJS

  10. 10

    How to render html in Angularjs directive?

  11. 11

    How to render raw html with AngularJS?

  12. 12

    AngularJS + Json: How to render html

  13. 13

    Angular JS render variable in scope function to view and make comparison

  14. 14

    How to bind a $scope variable to a normal variable in AngularJS?

  15. 15

    Render Razor variable inside AngularJS Html

  16. 16

    Render Razor variable inside AngularJS Html

  17. 17

    AngularJS: How to bind a $scope variable to another $scope.variable

  18. 18

    How to set default value for scope variable in angularjs

  19. 19

    AngularJS how to dynamically update part of scope with variable

  20. 20

    How to populate Angularjs $scope variable in javascript?

  21. 21

    How to check if a scope variable is undefined in AngularJS template?

  22. 22

    How to track scope variable changing in AngularJS

  23. 23

    How to asynchronously populate a $scope variable in AngularJS?

  24. 24

    How to set scope variable value in angularjs template

  25. 25

    How to set scope variable value in angularjs template

  26. 26

    How to update $scope variable value in AngularJs

  27. 27

    How to bind a $scope variable with text editor in angularjs

  28. 28

    how to store Specific Column in scope variable Angularjs

  29. 29

    how to access angularjs scope variable in javascript

HotTag

Archive