【发布时间】:2014-12-24 02:47:15
【问题描述】:
这个 angularJS 文件不会将表达式的值显示为“全名:John Doe”;而是显示“全名:{{firstName +”“+ lastName}}”。我可能错过了什么?
<!DOCTYPE html>
<html>
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div ng-app="" ng-controller="personController">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}
</div>
<script src="C:\Users\Vincent\Documents\Python\angularjs\StraightAngularJS\personController.js"></script>
</body>
</html>
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div ng-app="" ng-controller="personController">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}
</div>
<script src="personController.js"></script>
</body>
</html>
AngularJS 文件结束
下面是它的外部javaScript文件:personController.js
function personController($scope) {
$scope.firstName = "John",
$scope.lastName = "Doe",
$scope.fullName = function() {
return $scope.firstName + " " + $scope.lastName;
}
}
【问题讨论】:
-
尝试根据文档使用适当的控制器构造函数。已放弃对全局函数的支持。最好使用脚本文件的相对路径
-
在您开始想知道为什么脚本不起作用之前,请确保首先加载它!如果您使用的是 Chrome,请检查
sources选项卡以查找您的文件personController.js是否存在。顺便说一句,你为什么有两个body标签?...
标签: javascript html angularjs