我是AngularJS的新手。我有以下功能,其中我有一个FirstName字段和LastName字段(在两个不同的控制器中),然后单击按钮,我想在第三个Controller中显示FullName(名字+姓氏)。请建议我该怎么做。这是我的代码:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp">
<h2>Controller 1</h2><br />
<div ng-controller="FirstController">
<input type="text" ng-model="Data.FirstName">
<br>FirstName is : {{Data.FirstName}}</strong>
</div>
<hr>
<h2>Controller 2</h2><br />
<div ng-controller="SecondController">
<input type="text" ng-model="Data.LastName">
<br>LastName is : {{Data.LastName}}</strong>
</div>
<h2>Controller 3</h2><br />
<div ng-controller="ThirdController">
<button ng-click="getName(Data)">Get full Name</button>
FullName is : {{getName(Data)}}
</div>
</div>
</div>
<script>
var myApp = angular.module('myApp', []);
myApp.factory('Data', function () {
return { FirstName: '',LastName:'' };
});
myApp.controller('FirstController', function ($scope, Data) {
$scope.Data = Data;
});
myApp.controller('SecondController', function ($scope, Data) {
$scope.Data = Data;
});
myApp.controller('ThirdController', function ($scope, Data) {
$scope.Data = Data;
$scope.getName = function (Data)
{
$scope.Data = Data;
}
});
</script>
</body>
</html>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句