[英]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.