角度范围项目在项目更改后不适用
Angular scope items not applies after item change
我有项目服务、项目列表控制器和项目详细信息控制器:
.state('dashboard.items', {
url: '/items',
templateUrl: '/js/components/dashboard/items/items.html',
controller:'itemsListCtrl'
})
.state('dashboard.items.details', {
url: '/:id',
templateUrl: '/js/components/dashboard/items/itemDetails.html',
controller: 'itemDetailsCtrl',
resolve:{
items: function (ItemService) {
if(!ItemService.items)
ItemService.getAll().then(function (res) {
ItemService.items = res.data;
});
}
}
})
app.factory('ItemService', function ($http) {
var itemsFactory = {};
itemsFactory.getAll = function () {
return $http.get('/items');
}
itemsFactory.update = function () {
itemsFactory.items[0].name = "sadasd";
}
return itemsFactory;
})
app.controller('itemsListCtrl', function($scope, $state, ItemService){
if(!ItemService.items) {
ItemService.getAll().then(function (res) {
ItemService.items = res.data;
$scope.items = ItemService.items;
});
}else{
$scope.items = ItemService.items;
}
})
app.controller('itemDetailsCtrl', function ($scope, items, ItemService) {
$scope.item = ItemService.items[0];
$scope.item.name = "abc" ;
$scope.update = function(){
ItemService.update();
}
})
我有 ng 单击按钮,可以调用 edit() 函数。我使示例变得简单,在进行更新并编辑项目名称时,列表中显示的项目不会更改。我不知道我在这里错过了什么。该列表位于服务中,两个控制器都将其用于其目的。
我做错了什么?此方案的最佳做法是什么?
更新 1发现了一些奇怪的东西。当我在控制器初始化中编辑项目时,它会全局更改原始值。当它通过 edit() 方法发生时,它不会。什么好玩的?
谢谢。
$http.get 返回一个承诺,它将返回您的数据,所以在 .then 中你可以做你的事情,它会在完成后执行(异步)
$http.get('/items').then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
return response.data;//this is your data
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
相关文章:
- '填隙'当所述项目不在容器中时,将一些项目与引导程序网格对齐
- jquery ias插件不适用于我的网站,加载更多项目不起作用
- 如果项目不匹配,则显示变量
- CSS 类在特定场景中不适用
- ng类不适用
- Internet Explorer CSS类样式没有'如果存在具有相同类名的其他元素,则不适用
- 通过下拉列表框上的 appendChild 方法添加的项目不会显示在 IE8 下
- MONGO 查找数组中至少有一个项目不在另一个项目中的文档
- 指令“链接”不适用,如我所愿
- 反应.js:项目不起作用
- 最小和最大轴值在具有对数轴的高图表中不适用
- 角度范围项目在项目更改后不适用
- 页面加载顺序有问题 - 样式不适用
- 更改可见性后,背景颜色不适用
- CSS3 透视在动画完成之前不适用
- Jquery - 按 ID 选择项目不起作用
- 使用带有下划线循环的回调函数.js骨干.js项目不起作用
- 将最后一个子项移到第一个位置不适用jquery
- 当函数作为参数传递时,Javascript函数提升不适用
- 项目不't在超时时填充ng repeat时显示