-1

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?

1

1 Answer 1

1

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;
      }
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.