在Angular JS中POST后重新加载指令方法
Reload directive method after POST in Angular JS
我正在开发一个开源应用程序,它使用AngularJS作为前端的一部分,由于我希望它的工作方式(它是偏执的,为了保护起见,从dom中删除和添加内容),我遇到了一个我似乎无法解决的问题。
网站的主要内容通过一个服务加载,该服务从RESTneneneba API收集数据并填充指令,同一指令调用模板并呈现内容。
问题是,当添加新内容并且POST
成功时,我无法告诉指令重新加载内容,因为POST
调用来自不同$scope
中的控制器。我尝试过$watch
、$apply
、$push
,最近我一直在考虑$resource
,但我认为这不是解决方案。代码如下:
指令:
.directive("itemList", function ($timeout) {
return {
restrict: 'A',
template: "<ng-include src='getTemplateUrl()'/>",
replace: true,
controllerAs: 'items',
controller: function ($http, $scope, sikreAPIservice) {
$scope.getItems = function (categoryId) {
sikreAPIservice.getItemsbyCategory(categoryId)
.success(function (data, status) {
$scope.category_name = data.category_name;
$scope.category_id = data.category_id;
$scope.items = data.items;
$scope.lockedItem = false;
$timeout(function () {
$scope.lockedItem = true;
$.notify("View time expired. Locking...", "info");
$scope.getTemplateUrl();
}, itemTimeout);
})
.error(function (data, status) {
$.notify("Couldn't get the item data", "error");
});
};
$scope.getAllItems = function () {
sikreAPIservice.getItems()
.success(function (data) {
$scope.category_name = data.category_name;
$scope.category_id = data.category_id;
$scope.items = data.items;
$scope.lockedItem = false;
$timeout(function () {
$scope.lockedItem = true;
$.notify("View time expired. Locking...", "info");
$scope.getTemplateUrl();
}, itemTimeout);
})
.error(function (data, status) {
$.notify("Couldn't get the item data", "error");
});
};
$scope.getTemplateUrl = function () {
if ($scope.lockedItem) {
return '';
} else {
return 'includes/items.html';
}
$(document).foundation('reflow');
};
},
};
});
服务:
.factory('sikreAPIservice', function ($http) {
//var mainAPIUrl = 'https://api.sikr.io/v1/';
var sikreAPI = {};
sikreAPI.getItemsbyCategory = function (categoryId) {
return $http({
method: "GET",
url: mainAPIUrl + 'items?category=' + categoryId,
cache: false
});
};
});
进行POST的控制器:
.controller('ItemsCtrl', function ($scope, $compile, sikreAPIservice) {
$scope.additem = function (item) {
sikreAPIservice.createItem(item)
.success(function () {
$.notify("Item created", "success");
$scope.item = null;
$('#addItem').foundation('reveal', 'close');
})
.error(function () {
$.notify("Can't save the item", "error");
});
};
});
工作版本位于http://sikr.io/login.html(这是alpha!不要存储任何对你重要的东西,因为DB即将被清除)
源代码位于https://github.com/clione/sikre-frontend/tree/master/assets/js
我同意,也许整个应用程序的布局不是最好的,所以任何纠正它的指导都是受欢迎的。如果有人知道如何让指令在POST
之后重新加载内容,我们将不胜感激。
此解决方案由@Cerad的评论提供。
基本上,我必须创建一个只被指令捕获并重新加载内容的广播信号。
在控制器上:
$rootScope.$broadcast('updateItems', $scope.item.category);
关于指令:
$scope.$on('updateItems', function (event, data) {
$scope.getItems(data);
});
getItems是一个调用服务并获取数据的函数。
相关文章:
- jQuery加载方法字符集
- html5:一个很好的加载方法
- 检查浏览器是否支持iFrame上的加载方法
- ExtJS文件加载方法
- 陷入jQuery加载方法
- 使用jquery加载方法是一种好的做法吗
- JWPlayer在使用API加载方法时无法在iPad / iPhone上播放
- 如何在页面加载方法中调用 Javascript 函数
- HTML5 / Javascript图像加载方法和传递给变量
- ID 存储在 JS 变量中的 AJAX 加载方法
- 使用加载方法发送表单后隐藏“提交”按钮
- 在JQuery加载方法中使用post传递数据
- 如何调整jQuery加载的内容的大小's使用CSS的加载方法
- jQuery()在for循环内部加载方法,如何在出现错误时退出
- 重新加载方法:响应文本与窗口.位置.href(1 次旅行与 2 次)
- 使用Jquery加载方法与Slick插件
- Jquery加载方法上传多个页面
- Php包含方法和Jquery加载方法
- 将焦点设置在使用jQuery加载方法导入的其他页面的元素上
- 触发器以编程方式选择加载方法