简体   繁体   中英

MVC view button click event , Pass AngularJs object to another MVC Controller

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?

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> 

$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});
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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