范围数据不绑定到{{}}

时间:2018-01-28 20:43:43

标签: javascript angularjs

我尝试使用角度数据绑定{{}}技术将范围数据绑定到HTML,但我将POSTS作为输出,我尝试在堆栈网站中引用文章,但无法计算出了什么问题,尝试了$ scope.apply但也没有运气!建议我修复一下!

(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)
            .success(getAllPosts);
    }
}
})();

我在HTML标记中添加了ng-app

<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="title"> </textarea>
    <button ng-click="createPost(post)" class="btn btn-primary btn-block">Post</button>

    {{posts}}

</div>
</body>

0 个答案:

没有答案