Javascript 删除操作后的AngularJS更新视图
我有一个AngularJS和Rails应用程序,可以执行RESTful操作:创建、读取、销毁 在执行“删除”操作后,我必须刷新页面,并试图找出如何异步更新视图 “创建”操作使用.success http函数更新视图中的单词;我尝试过用delete执行类似的方法,但得到一个错误:Javascript 删除操作后的AngularJS更新视图,javascript,ruby-on-rails,angularjs,Javascript,Ruby On Rails,Angularjs,我有一个AngularJS和Rails应用程序,可以执行RESTful操作:创建、读取、销毁 在执行“删除”操作后,我必须刷新页面,并试图找出如何异步更新视图 “创建”操作使用.success http函数更新视图中的单词;我尝试过用delete执行类似的方法,但得到一个错误:无法读取未定义的属性“success” app.js //= require angular-rails-templates //= require_tree . angular.module('d-angular',
无法读取未定义的属性“success”
app.js
//= require angular-rails-templates
//= require_tree .
angular.module('d-angular', ['ui.router', 'templates'])
// Set routing/configuration
// ------------------------------
.config(['$stateProvider', '$urlRouterProvider',
// Set state providers
function($stateProvider, $urlRouterProvider) {$stateProvider
// Home state
.state('home', {
url: '/home',
templateUrl: 'home.html',
controller: 'MainCtrl',
resolve: {
listPromise: ['lists', function(lists){
return lists.getAll();
}]
}
})
$urlRouterProvider.otherwise('home');
}
])
// lists factory
// Factories are used to organize and share code across the app.
// ------------------------------
.factory('lists', ['$http',
function($http){
// create new obect with array of lists
var o = { lists: [] };
// get all lists
o.getAll = function() {
return $http.get('/lists.json').success(function(data){
angular.copy(data, o.lists);
});
};
// get specific list
o.get = function(id) {
return $http.get('/lists/' + id + '.json').then(function(res){
return res.data;
});
};
// create list
o.create = function(post) {
return $http.post('/lists.json', post).success(function(data){
o.lists.push(data);
});
};
// delete list
o.delete = function(id) {
$http.delete('/lists/' + id + '.json');
}
// add word to list
o.addWord = function(id, word) {
return $http.post('/lists/' + id + '/words.json', word);
};
o.deleteWord = function(id, word) {
$http.delete('/lists/' + id + '/words/' + word + '.json');
}
return o;
}
])
// Main controller
// ------------------------------
.controller('MainCtrl', ['$scope', '$stateParams', 'lists', '$http',
// Main scope (used in views)
function($scope, $stateParams, lists, $http) {
// array of lists
$scope.lists = lists.lists;
$scope.list = lists.lists[$stateParams.id];
// Add list function
// Creates a new list
$scope.addList = function(){
// prevent empty titles
if(!$scope.title || $scope.title === '') {
return;
}
lists.create({
title: $scope.title,
date: new Date().toJSON().slice(0,10),
});
// reset title
$scope.title = '';
};
$scope.deleteList = function() {
lists.delete($scope.list.id).then(function(){
$scope.lists.splice(index, 1)
}); // UPDATE THE VIEW
};
// Add word function
$scope.addWord = function(){
// push new word to array
lists.addWord($scope.list.id, {
title: $scope.word,
date: new Date().toJSON().slice(0,10),
})
.success(function(word) {
$scope.list.words.push(word);
});
});
};
$scope.deleteWord = function(word_id) {
lists.deleteWord($scope.list.id, word_id);
};
}
]);
看法
添加
选择列表
删除
{{list.id}
添加
{{word.title}
{{word.id}
删除
是否因为您没有从
$http.delete()
返回值?在工厂函数中添加return
语句:
o.deleteWord = function(id, word) {
return $http.delete('/lists/' + id + '/words/' + word + '.json');
}
然后在控制器中,您可以引用.success()
:
澄清我的评论
假设您已经有了一个o.getAll函数,您只需在成功回调中调用它即可
o.deleteWord = function(id, word) {
return $http.delete('/lists/' + id + '/words/' + word + '.json')
.success(function(response){
return o.getAll();
}):
}
我会把它放在o.deleteWord函数中。此外,在rails控制器中,您还需要在销毁模型后使用Word.all来响应。否则,页面将不会更新,因为它将侦听更新的words.json
def destroy
Word.delete(params[:id])
respond_with Word.all
end
假设您已经有了一个o.getAll函数,您只需在成功回调中调用它即可
o.deleteWord = function(id, word) {
return $http.delete('/lists/' + id + '/words/' + word + '.json')
.success(function(response){
return o.getAll();
}):
}
def destroy
Word.delete(params[:id])
respond_with Word.all
end