I have got two controllers and want them to send data from the first one to the second one without creating a factory.
module.controller('UpdatesController', function ($scope) {
var update = {};
update.items = [{
title: 'Company goes high',
date: '24-11-2015',
excerpt: 'Labore et dolore magna aliqua',
desc: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
picture: 'images/01.jpg',
}];
$scope.items = update.items;
$scope.showUpdate = function (index) {
var selectedItem = update.items[index];
update.selectedItem = selectedItem;
$scope.navi.pushPage('update.html', {
title: selectedItem.title,
date: selectedItem.date,
excerpt: selectedItem.excerpt,
desc: selectedItem.desc,
picture: selectedItem.picture
});
};
});
module.controller('UpdateController', function ($scope, items) {
$scope.item = items;
});
But it seems not working. The error is: Uncaught (in promise) Error: [$injector:unpr]
Can please anyone help?
In AngularJS, a controller
can not be injected to other controller
. If you want the inter-connectivity / sharing in between controllers then you should use factory or service.
In your case, you can do
module.controller('UpdatesController', ['$scope', 'TestFactory', function ($scope, TestFactory) {
var update = {};
update.items = [{
title: 'Company goes high',
date: '24-11-2015',
excerpt: 'Labore et dolore magna aliqua',
desc: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
picture: 'images/01.jpg',
}];
$scope.items = update.items;
TestFactory.setItems($scope.items);
$scope.showUpdate = function (index) {
var selectedItem = update.items[index];
update.selectedItem = selectedItem;
$scope.navi.pushPage('update.html', {
title: selectedItem.title,
date: selectedItem.date,
excerpt: selectedItem.excerpt,
desc: selectedItem.desc,
picture: selectedItem.picture
});
};
}]);
module.controller('UpdateController', ['$scope', 'TestFactory', function ($scope, TestFactory) {
$scope.item = TestFactory.getItems();
}]);
module.factory('TestFactory', function(){
return {
getItems : function(){
return this.items;
},
setItems : function(items){
return this.items = items;
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.