Html Code
<div ng-app="myApp">
<div ng-controller="cntrl1">
{{title}}
<br /><br />
<span>{{ReceivedData}}</span>
</div>
<br />
<hr />
<div ng-controller="cntrl2">
{{title}}
<input ng-model="sampledata" />
<button ng-click="SendData()">Send</button>
</div>
</div>
<script src="angular.min.js"></script>
<script src="index.js"></script>
Index.js Code - AngularJs Code
/// <reference path="angular.min.js" />
var app = angular.module("myApp", []);
app.controller("cntrl1", function ($scope, $rootScope, demoService) {
$scope.title = "Controller 1";
$rootScope.$on("dummyevent", function () {
var t = demoService.GetData();
$scope.ReceivedData = t;
});
});
app.controller("cntrl2", function ($scope, demoService) {
$scope.title = "Controller 2";
$scope.SendData = function () {
var d = $scope.sampledata;
demoService.SetData(d);
}
});
app.service("demoService", function ($rootScope) {
this.TempData = "";
this.SetData = function (d) {
this.TempData = d;
$rootScope.$emit("dummyevent")
}
this.GetData = function () {
return this.TempData;
}
})
easy to understand thank you
ReplyDeleteReally easy to understand A big thank you
ReplyDeletevery nice taklu boy
ReplyDelete