How broadcast data of HTTP post method in angularjs -
i'm using same http method in different controller following sample:
service:
var method="samplemethod" hotalstatisticservice.getreservations = function (data) { return $http({ method: 'post', data: data, cache: false, url:'http://sample.com/'+method }); } first controller
.controller("sampleactrl", function ($scope,hotalstatisticservice ) { hotalstatisticservice.getreservations({start:start, end:end, type:type}) .success(function (data) { $scope.samplea=data; }) } second controller
.controller("samplebctrl", function ($scope,hotalstatisticservice ) { hotalstatisticservice.getreservations({start:start, end:end, type:type}) .success(function (data) { $scope.sampleb=data; }) } how use here method in 1 controller?
let me other solution uses factories better solution.using services option.
another ,perhaps crude way using $rootscope.answer below.
what want share data between 2 controllers. based on reply comments, both controllers belong same module.you can use $rootscope here act common point.
as can see, i've added $rootscope dependency in both controllers , printed txt variable in second div.
js code
var app = angular.module('plunker', []); app.controller('actrl', function($scope,$rootscope) { $scope.name = 'this controller '; $scope.execute = function() { alert('executed!'); } $rootscope.txt="hi there other side"; }); app.controller('bctrl', function($scope,$rootscope) { $scope.name = 'this controller b '; }); html
<div ng-controller="actrl"> <p>hello {{name}}!</p> </div> <div ng-controller="bctrl"> <p>hello {{name}}!</p> {{txt}} </div> here demo
Comments
Post a Comment