我有這些文件:不能綁定範圍內的變量在angularjs
的index.html:
<!DOCTYPE html>
<html>
<head>
<title>Gestore Anagrafica</title>
</head>
<body>
<div>
<h3>Insert new person</h3>
<div ng-app="Person" ng-controller="PersonController">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
Age: <input type="number" ng-model="age"><br>
<br>
Full Name: {{fullName()}} <br>
Is major: {{isMajor()}} <br>
<button ng-click="add()">Add Person</button>
</div>
</div>
<script type="text/javascript" src="js/angular.js"></script>
<script type="text/javascript" src="js/RegistryService.js"></script>
<script type="text/javascript" src="js/PersonController.js"></script>
</body>
</html>
JS/RegistryService.js:
angular.module('RegistryService', []).
service('registry', function()
{
this.people = [];
this.add = function(person)
{
people.push(person);
}
});
JS/PersonController.js:
var app = angular.module('Person', ['RegistryService']);
app.controller('PersonController',['registry', function($scope, registry) {
$scope.firstName = "testName";
$scope.lastName = "";
$scope.age = 20;
$scope.isMajor = function()
{
return $scope.age > 18;
};
$scope.add = function()
{
registry.add({ 'firstName': $scope.firstName,
'lastName': $scope.lastName,
'age': $scope.age});
};
$scope.fullName = function()
{
return $scope.firstName + " " + $scope.lastName;
};
}]);
綁定不會發生:當我更改名稱或年齡沒有任何反應,而且在開始時輸入都是空白的,但我期望在age中看到20和testName。瀏覽器的控制檯顯示沒有錯誤。我錯過了什麼嗎?
約束條件:服務'RegistryService'必須位於另一個文件中。
問題: 與這兩條線
var app = angular.module('Person', ['RegistryService']);
app.controller('PersonController',['registry', function($scope, registry) {
我告訴那個叫人模塊需要一種叫RegistryService工作,控制器PersonController將使用位於進RegistryService的「註冊表」的事(因此,如果我把這裏沒有定義到RegistryService的東西是一個錯誤)。函數($ scope,registry)是控制器的構造函數,它使用全局變量$ scope和從'RegistryService'取得的變量註冊表。我對依賴注入的整體理解是否良好?