Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我必须刷新AngularJS页面才能看到我的更改_Javascript_Angularjs_Angularjs Scope - Fatal编程技术网

Javascript 我必须刷新AngularJS页面才能看到我的更改

Javascript 我必须刷新AngularJS页面才能看到我的更改,javascript,angularjs,angularjs-scope,Javascript,Angularjs,Angularjs Scope,我必须刷新页面才能看到更改,Angular是一个双向绑定,但对于我的问题来说,它的行为似乎是单向的。我从一个小时前就开始试图解决这个问题,但找不到任何解决办法 index.html <div ng-controller="tweetController as tweet"> <form> <!-- USERNAME INPUT --> <div class="form-group">

我必须刷新页面才能看到更改,Angular是一个双向绑定,但对于我的问题来说,它的行为似乎是单向的。我从一个小时前就开始试图解决这个问题,但找不到任何解决办法

index.html

<div ng-controller="tweetController as tweet">
            <form>  <!-- USERNAME INPUT -->
                <div class="form-group">
                    <label>Title</label>
                    <input type="text" class="form-control" ng-model="tweet.tweetData.title">
                </div>

                <!-- PASSWORD INPUT -->
                <div class="form-group">
                    <label>Tweet Content</label>
                    <input type="text" class="form-control" ng-model="tweet.tweetData.content">
                </div>

                <!-- LOGIN BUTTON -->
                <button ng-click= "tweet.createTweet()" type="submit" class="btn btn-block btn-primary">
                    <span>Create Tweet</span>
                </button>

        </form>

        <tr ng-repeat="tw in tweet.tweets">
                    <td>{{ tw._id }}</td>
                    <td>{{ tw.title }}</td>
                    <td>{{ tw.content }}</td>                   
        </tr>

    </div>
service.js

angular.module('tweetService', [])


.factory('Tweet', function($http) {

    // get all approach
    var tweetFactory = {};

    tweetFactory.all = function() {
        return $http.get('/api/');
    }


    tweetFactory.create = function(tweetData) {
        return $http.post('/api/', tweetData);
    }

    return tweetFactory;

});

您需要将新的
tweet
推送到您的
tweets
集合中-该集合不会自动更新。据我所知,您只能在页面加载时调用
.all()
,因此在
创建完成后,只需推送到数组即可:

vm.createTweet = function() {
    vm.processing = true;

    vm.message = '';

    Tweet.create(vm.tweetData) 
        .success(function(data) {
            vm.processing = false;

            // clear the form
            vm.tweetData = {}
            vm.message = data.message;

            //show the new tweet in the view
            vm.tweets.push(data); //assuming "data" is the returned tweet
        });
};

你能做一把小提琴吗?这个问题你能做一把小提琴吗
vm.createTweet = function() {
    vm.processing = true;

    vm.message = '';

    Tweet.create(vm.tweetData) 
        .success(function(data) {
            vm.processing = false;

            // clear the form
            vm.tweetData = {}
            vm.message = data.message;

            //show the new tweet in the view
            vm.tweets.push(data); //assuming "data" is the returned tweet
        });
};