<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl2" ng-init="person = new Person('John','Doe')">
First Name: <input type="text" ng-model="person.firstName"><br>
Last Name: <input type="text" ng-model="person.lastName"><br>
<br>
Full Name: {{person.sayGreeting('1')}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl2', function ($scope) {
function Person(fName,lName){
this.firstName = fName;
this.lastName = lName;
this.sayGreeting = function(lan){
if(lan=="1") return '1 : ' + this.firstName+' '+this.lastName;
else return '2: ' + this.firstName+' '+this.lastName;
}
}
}
});
</script>
</body>
</html>
&#13;
答案 0 :(得分:1)
这是一个经过一些修改的代码,我希望它可以帮到你。
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<div ng-app="myapp" ng-controller="myCtrl2" ng-init="person = Person('John','Doe')">
First Name: <input type="text" ng-model="person.firstName"><br>
Last Name: <input type="text" ng-model="person.lastName"><br>
<br>
Full Name: <label>{{sayGreeting('1')}}</label>
</div>
<script>
var app = angular.module('myapp', []);
app.controller('myCtrl2', function ($scope) {
$scope.person={};
$scope.Person= function(fName,lName){
$scope.person.firstName = fName;
$scope.person.lastName = lName;
return $scope.person;
}
$scope.sayGreeting = function(lan){
if(lan=="1") return '1 : ' + $scope.person.firstName+' '+ $scope.person.lastName;
else return '2: ' + $scope.person.firstName+' '+ $scope.person.lastName;
}
});
</script>
</body>
</html>