简体   繁体   English

如何将值传递给其他控制器?

[英]How to pass the value into other controller?

I don't think this information is enough to completely make sense but I just some direction on this. 我认为这些信息不足以完全理解,但我只是对此提供了一些指导。 or if you want I can share the complete code with you in floobit 或者如果您愿意,我可以与您共享完整的代码

I am trying to put this array from API into a seller array. 我正在尝试将此数组从API放入卖方数组。

    this.fetchSellers = function () {
        var date = new Date().toISOString();
        $http.get('/api/sellers', {params: {auctionDate: date}})
            .success(function (sellers) {
                sellers && sellers.length && ($scope.sellers = sellers);
            });
    };

I am trying to put this one of the value Seller.symbol into the array list below. 我试图将值Seller.symbol之一放入下面的数组列表中。 在此处输入图片说明

$scope.sellers = []

It able to load into ng-option like 它能够像这样加载到ng-option

<select class="form-control" ng-model="auction.seller" ng-options="o.id as o.symbol for o in sellers" required></select>

在此处输入图片说明

But the question is, I am not able to pass the value into another controller in the table. 但是问题是,我无法将值传递到表中的另一个控制器。
在此处输入图片说明

code for HTML HTML代码

   <tbody class="auction-group" ng-repeat="a in auctions">
    <tr>
        @*auction *@
    <td>{{ acv('auctionDate', a) }}</td>
        @*seller*@
    <td>{{ a.seller }}</td>

Code for ng ng代码

 this.tableComplexFieldDict = {

        auctionDate: {
            label: 'Auction',
            cacheKey: 'auctionDate',
            getCacheValue: function (a) {
                return moment(a.startAt).format('MM/DD/YY');
            }
        },

        facility: {
            label: 'Facility',
            cacheKey: 'facility',
            getCacheValue: _.property('contract.facility')
        },

        seller: {
            label: 'Seller',
            cacheKey: 'seller',
            getCacheValue: function (a) {
                return a.seller;
            }
        },

in order to do that you have to use another service. 为此,您必须使用其他服务。 Ideally, you can pass data between controller to service vice versa. 理想情况下,您可以在控制器之间传递数据,反之亦然。 so just create another service and include it in which ever controller you want to pass data to. 因此,只需创建另一个服务并将其包含在您要向其传递数据的控制器中即可。

here is example 这是例子

or 要么

 // declare the app with no dependencies var myApp = angular.module('myApp', []); myApp.factory('Data', function(){ return { FirstName: '' }; }); myApp.controller('one', function( $scope, Data ){ $scope.Data = Data; }); myApp.controller('two', function( $scope, Data ){ $scope.Data = Data; }); 
 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> <script src="https://rawgit.com/angular/bower-angular/master/angular.min.js"></script> </head> <body> <div ng-app="myApp"> <h3>write something in text box it will be printed by both controller</h3> <div ng-controller="one"> <input type="text" ng-model="Data.FirstName"><!-- Input entered here --> <br>Controller 1 : <strong>{{Data.FirstName}}</strong><!-- Successfully updates here --> </div> <hr> <div ng-controller="two"> Controller 2: {{Data.FirstName}}<!-- How do I automatically updated it here? --> </div> </div> </body> </html> 

Passing data between controllers can be done in 3 ways: 可以通过3种方式在控制器之间传递数据:

  • Shared service both controllers use with set get data functions 两个控制器都使用带有设置获取数据功能的共享服务

  • Using direct data binding through root scope as a medium to store read data from 使用通过根范围的直接数据绑定作为介质来存储从中读取的数据

(terrible abusage of root scope) (可怕地侵犯了根范围)

  • through events 通过事件

For me, best practice is Using a store and read service ;) 对我来说,最佳实践是使用存储和读取服务;)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM