-1
我有以下要求。使用按鈕點擊另一個控制器的值
我有三個控制器,CTRL 1,CTRL 2和CTRL 3.我想傳遞值firstName到Ctrl 1,姓氏到CTRL2並使用按鈕單擊事件從CTRL3獲取全名。
我試過我的下面的代碼,但我得到輸出爲NaN。 請幫忙。
<script>
var myApp = angular.module('myApp', []);
myApp.factory('data', function() {
var factory = {};
var firstName = '';
var secondName = '';
factory.getName = function() {
return firstName + ' ' + secondName;
}
return factory;
});
myApp.controller('FirstController', function ($scope, data) {
$scope.firstName = data.firstName
});
myApp.controller('SecondController', function ($scope, data) {
$scope.secondName = data.secondName;
});
myApp.controller('ThirdController', function ($scope, data) {
$scope.getFullName = function() {
$scope.fullName = data.getName();
}
});
</script>
<h2>Controller 1</h2><br />
<div ng-controller="FirstController">
<input type="text" ng-model="data.firstName">
<br>FirstName is : {{data.firstName}}
</div>
<hr>
<h2>Controller 2</h2><br />
<div ng-controller="SecondController">
<input type="text" ng-model="data.secondName">
<br>LastName is : {{data.secondName}}
</div>
<h2>Controller 3</h2><br />
<div ng-controller="ThirdController">
<button ng-click="getFullName()">Get full Name</button>
Full name is: {{fullName}}
</div>
</div>
使用的服務。 –