Skip to main content
Post Closed as "Duplicate" by Stewie, Cjxcz Odjcayrwl, Riccardo Marotti, Rubens, Aurelio De Rosa
Source Link
user2464884
user2464884

Passing value from one control to another in angular js

I'm writing a program to display the text from the text-box to a box. This is done using angular js. I'm stuck at the portion where, i can't pass the value from one controller to another. Here's the code :

<html ng-app="MyApp">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
<script data-require="[email protected]" src="http://code.angularjs.org/1.1.5/angular.min.js" data-semver="1.1.5"></script>
<script src="app.js"></script>
</head>

<body>
<div class="container">
<div class="box" ng-controller="OutputController">
       {{output.text}}
</div>

<div class="row" ng-controller="InputController">
  <input class="input-text" ng-model="output.text">
  <button class="btnoutput btn-primary" ng-click="onClick()">Click</button>
</div>
</div>    
</body>

</html> 

JS:-

angular.module("MyApp",[]);

var application = angular.module("MyApp");

// Input
angular.module("MyApp").controller("InputController",function($scope,Share){
  //$scope.output = Share;
  $scope.onClick = function(){
  $scope.output.text = "Hello " ;
 }
});



// Output
angular.module("MyApp").controller("OutputController",function($scope,Share){
  $scope.output = Share;
});



// Share the data
angular.module("MyApp").factory("Share",function(){
  var object = {};
  object.text = "";
  return object;
});

Until now, the value is getting displayed inside the box, while i type.That is at the same time. But i want to display the text only when i click the button. Thank You