简体   繁体   English

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

[英]I Have to refresh the AngularJS page to see my changes

I have to refresh the page to see changes, Angular is a two-way binding but seems for my problem it is behaving like one-way. 我必须刷新页面才能看到更改,Angular是双向绑定,但是对于我的问题看来它的行为像单向一样。 I've been trying to solve this problem since 1 hour ago and couldn't find any solution to it. 从1小时前开始,我一直在尝试解决此问题,但找不到任何解决方案。

index.html 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>

tweetCtrl.js tweetCtrl.js

angular.module('tweetCtrl', ['tweetService'])

.controller('tweetController', function(Tweet) {

    var vm = this;

    Tweet.all()
        .success(function(data) {
            vm.tweets = data;
        });


    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;
            });
    };

});

service.js 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;

});

You need to push your new tweet to your tweets collection - that won't automatically update. 您需要将新的tweet送到您的tweets文集-不会自动更新。 As far as I can see, you only ever call .all() on page load, so simply push to the array once your create is done: 据我所知,您只能在页面加载时调用.all() ,因此只需在create完成后将其压入数组即可:

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
        });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular.js必须刷新页面才能看到更改 - Angularjs must refresh page to see changes 每次看到更改我都必须清除缓存并刷新 - Every time to see changes I have to clear cache and refresh Highchart - 我必须刷新页面才能看到新数据 - Highchart - I have to refresh the page to see new data angularjs当我刷新页面时,它会将我重定向到登录页面 - angularjs When I refresh my page it redirects me to login page 当我尝试刷新页面时出现此错误 - When i try to refresh my page i have this error 我必须刷新我的Greasemonkey脚本的页面才能运行? - I have to refresh the page for my Greasemonkey script to run? 在AngularJS中刷新时是否应该看到相同的内容 - Should I see the same thing when I refresh in AngularJS 为什么突然间,我必须推动 GIT 才能看到我的 CSS 更改? - Why, all of a sudden, do I have to push GIT to see my CSS changes? ReactApp-当我在VS代码编辑器中将js文件中的更改保存时,浏览器不会刷新页面。 - ReactApp - The browser does not refresh the page when i save changes in my js file in VS code editor . 为什么我必须在页面中禁用javascript才能看到mailchimp表单正常工作? - why I have to disable javascript in my page to see my mailchimp form works?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM