0
我需要一個提示如何連接兩個控制器在Rails中的不同視圖。 我在我的索引中有物品和購物車,當我點擊新選項卡中的物品並將物品添加到購物車時,我想將此物品添加到購物車中,無需刷新它。Angularjs通信控制器在不同的意見 - Rails
app.controller('LineItemsController', ['$scope', '$rootScope', '$controller', 'LineItems',
function($scope, $rootScope, $controller, LineItems) {
$controller('CartsController', {
$scope: $scope
});
$scope.fetchLineItems = function() {
LineItems.get({
cartId: cartId
}).then(
function(results) {
$scope.lineItems = results;
},
function(error) {}
)
};
$scope.fetchLineItems();
$scope.$on('scanner-started', function(event, args) {
console.log("alert");
$scope.fetchLineItems();
});
}
]);
app.controller('LineItemController', ['$scope', '$rootScope', '$controller', 'LineItem',
function($scope, $rootScope, $controller, LineItem) {
$controller('LineItemsController', {
$scope: $scope
});
$scope.addItem = function() {
new LineItem({
itemId: window.Id
}).create().then(
function(results) {
$scope.linkUpdated = true;
$rootScope.$broadcast('scanner-started');
},
function(error) {
$scope.errors = true;
$scope.linkUpdated = false;
}
)
};
}
]);
app.factory('LineItem', ['railsResourceFactory',
function(railsResourceFactory) {
return railsResourceFactory({
url: '/items/{{itemId}}/line_items',
title: 'line_item'
});
}
]);
app.factory('LineItems', ['railsResourceFactory',
function(railsResourceFactory) {
return railsResourceFactory({
url: '/carts/{{cartId}}/line_items',
title: 'line_item'
});
}
]);
我的代碼看起來像這樣,我添加了$ broadcast和$ on,但它僅適用於我的show view,而不是index。
有沒有像$ scope刷新或類似的東西,我可以給我的索引?