0

I have to pass AngularJs object to another Controller when button click event on view.

CHTML

<div ng-repeat="hotel in respData.hotels">
   <button type="button" class="btn" data-ng-click="setTab(hotel.code)">Check Availability
</button></div>

Script

$scope.setTab = function (hotelcode) {
   var url = 'Hotel';
   url += '?HotelCode=' + hotelcode ' 
   window.location.href = url;}

Now i'm passing one value only. I want to pass hotel object as a parameter. Is their way to do that?

4
  • Why are you using window.location to change url , why you are not using angular routing ? Commented Mar 22, 2017 at 6:48
  • I'm using mvc routing. if their way to combined anguler and mvc routing? Commented Mar 22, 2017 at 7:07
  • this blog may be helpful for you then c-sharpcorner.com/UploadFile/cd7c2e/… Commented Mar 22, 2017 at 7:52
  • Thank you, That's very helpful. Commented Mar 22, 2017 at 8:32

2 Answers 2

2

You can pass your whole Hotel object to your first controller and then make use of $emit or $broadcast in order to send that object to another controller. Here's a short example:

One.html

<html ng-app="app">

<script src="http://code.angularjs.org/1.2.13/angular.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js"></script>

 <script>
 var app = angular.module('app', ['ui.router']).config(function($stateProvider, $urlRouterProvider){
 $stateProvider.state('two',{
   url: '/two',
   templateUrl: "two.html",
 })
 })

 app.controller("Parent", function ($scope, $state) {
   $scope.send = function (msg) {
     $scope.$broadcast('eventName', { message: msg });
     $state.go('two')
   };
 });

 app.controller("Child", function ($scope) {
   $scope.$on('eventName', function (event, args) {
     $scope.message = args.message;
     console.log($scope.message);
   });
 });

 </script>

<body ng-app="app">
  <h1> Index Page </h1>

  <!---
  Look at these div tags here, $broadcast is used to transfer content from
  Parent to Child Controllers only and $emit for Child to Parent Controller
  !--->

  <div ng-controller = "Parent" >
  	 <button ng-click = send('Hello')> Send Hello</button>
         <div ng-controller = "Child" class="container" ui-view> </div>
  </div>

</body>
</html>

Two.html

<body ng-app="app">
  	 <span> Recieved : {{message}}</span>
</body>

Sign up to request clarification or add additional context in comments.

5 Comments

I'm try that before. return null object on other view. Is there sample with 2 html pages?
Try the code I gave above, just send your hotel object instead of msg to the Child Controller.
Make 2 different html files and copy the above code and try running it.
XMLHttpRequest cannot load second html page Cross origin requests. how can i correct that?
Either use firefox or make a node server and deploy these web pages on it.
1
$state.go('toState',object);  

You can use $state.go to send object values to another controller if you are using ui-router if not you can use emit and broadcast see details here

Working plnkr of broadcast can be found here

app.controller('Parent', function($scope) {
  $scope.message="";
   $scope.emitedmessage="";
  $scope.clickevent=function(){
    $scope.$broadcast('transfer',{message:$scope.message});
  }
    $scope.$on('transferUp',function(event,data){
    console.log('on working');
     $scope.emitedmessage=data.message;
  });
});

app.controller('Child',function($scope){
  $scope.message="";
   $scope.broadcastmessage=""
  $scope.$on('transfer',function(event,data){
     $scope.broadcastmessage=data.message;
  });
  $scope.clickevent=function(){
    $scope.$emit('transferUp',{message:$scope.message});
  }
});

1 Comment

Are your sure that OP is using ui.router?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.