Fenici
Fenici

Reputation: 461

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.

    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. enter image description here

$scope.sellers = []

It able to load into ng-option like

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

enter image description here

But the question is, I am not able to pass the value into another controller in the table.
enter image description here

code for 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

 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;
            }
        },

Upvotes: 0

Views: 125

Answers (2)

CodeSmith
CodeSmith

Reputation: 3197

Passing data between controllers can be done in 3 ways:

  • 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 ;)

Upvotes: 1

Tarang Rathod
Tarang Rathod

Reputation: 98

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>

Upvotes: 1

Related Questions