0
在下面的代码this example:
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="personCtrl">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{fullName()}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
$scope.fullName = function() {
return $scope.firstName + " " + $scope.lastName;
};
});
</script>
</body>
</html>
如何$scope.fullName
被设置为使用$scope
的功能,但并没有明确地传递了吗?
I.e.为什么:
$scope.fullName = function() {
,而不是
$scope.fullName = function($scope) {
'$ scope'已经被注入你的'controller'函数。 – developer033
这是javascript的基础知识。阅读关于范围和功能参数。 – dfsq
^这。只有函数被参数调用(这里不是:'{{fullName()}}'),它必须是'function($ scope){}'。 '$ scope'不应该传递给这个函数,因为'$ scope'已经在父函数作用域中可用。如上所述,这些都是JS基础知识,它们并不特定于Angular。 – estus