我正在编写一个程序来将文本框中的文本显示到一个框中。这是使用 Angular js 完成的。我被困在无法将值从一个控制器传递到另一个控制器的部分。这是代码:
<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="angular.js@1.1.x" 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;
});
到现在为止,值在我输入的同时显示在框内。那是在同一时间。但我只想在单击按钮时显示文本。谢谢你