简体   繁体   English

使用 AngularJS 从一个控制器获取数据到另一个控制器

[英]Getting data from one controller to another with AngularJS

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]错误是:未捕获(承诺)错误:[$injector:unpr]

Can please anyone help?可以请任何人帮忙吗?

In AngularJS, a controller can not be injected to other controller .在 AngularJS 中,一个controller不能注入其他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;
      }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个AngularJS控制器获取数据 - Getting Data From Another AngularJS Controller 如何在angularjs中将对象数据从一个控制器传递到另一个控制器 - how to pass object data from one controller to another controller in angularjs 如何将数据从一个控制器发送到angularjs中的另一个控制器 - How to send data from one controller to another controller in angularjs 在AngularJS中将数据从一个控制器传递到另一个控制器 - Passing data from one controller to another controller in AngularJS 将 JSON 数据从一个控制器传递到另一个控制器 angularJS - passing JSON data from one controller to another controller angularJS 使用工厂将数据从一个控制器传递到AngularJS中的另一个控制器 - Using a factory to pass data from one controller to another controller in AngularJS AngularJS - 自动将数据从一个控制器更新到另一个控制器 - AngularJS - Auto update data from one controller to another AngularJS:使用同一控制器将数据从一个视图传递到另一个视图 - AngularJS : Pass data from one view to another with in same controller 在一个控制器angularjs中将数据从作用域获取到另一个作用域 - Get data from scope to another scope within one controller angularjs 将angularjs网格数据从一个控制器公开到另一个控制器 - exposing angularjs grid data from one controller into another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM