我从O'Reilly Book of AngularJS中复制了这段代码 但它没有给出与本书相同的结果。 问题在于,它没有与控制器模型结合使用
<html ng-app>
<body>
<form ng-controller="StartUpController">
Starting: <input ng-change="computeNeeded()"
ng-model="funding.startingEstimate">
Recommendation: {{funding.needed}}
</form>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script>
function StartUpController($scope) {
$scope.funding = { startingEstimate: 0 };
$scope.computeNeeded = function() {
$scope.needed = $scope.startingEstimate * 10;
};
}
</script>
</body>
</html>
答案 0 :(得分:0)
使用this
ie,
这样this.funding
答案 1 :(得分:0)
问题出在你的角度文件中,试试这个
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app>
<body>
<form ng-controller="StartUpController">
Starting: <input type="number" ng-change="computeNeeded()"
ng-model="funding.startingEstimate">
Recommendation: {{needed}}
</form>
<script>
function StartUpController($scope) {
$scope.funding = { startingEstimate: 0 };
$scope.computeNeeded = function() {
console.log("Hai");
$scope.needed = $scope.funding.startingEstimate * 10;
};
};
</script>
</body>
</html>
&#13;
答案 2 :(得分:0)
使用带有angular1.2版本的ng-app的方式问题。 在angular1.2中,一旦添加了带有标记的ng-app指令,就可以直接将函数构造函数注入到ng-controller中。 你正在做什么,但使用angular1.4版本。使用angular1.4,您必须首先创建角度模块然后注册您的控制器 与该模块。一旦你这样做,那么只有你可以用ng-controller绑定控制器。
Angular1.2方式:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app>
<body>
<form ng-controller="StartUpController">
Starting: <input type="number" ng-change="computeNeeded()"
ng-model="funding.startingEstimate">
Recommendation: {{needed}}
</form>
<script>
function StartUpController($scope) {
$scope.funding = { startingEstimate: 0 };
$scope.computeNeeded = function() {
console.log("Hai");
$scope.needed = $scope.funding.startingEstimate * 10;
};
};
</script>
</body>
</html>
Angular1.4或+方式:
<html ng-app='mainApp'>
<body>
<form ng-controller="StartUpController">
Starting: <input ng-change="computeNeeded()"
ng-model="this.funding.startingEstimate">
Recommendation: {{this.funding.needed}}
</form>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script>
var jimApp = angular.module("mainApp", []);
jimApp.controller('StartUpController', function($scope){
$scope.funding = { startingEstimate: 0 };
$scope.computeNeeded = function() {
$scope.needed = $scope.startingEstimate * 10;
}
});
</script>
</body>
</html>
答案 3 :(得分:0)
将ng-模型从this.funding.startingEstimate更改为仅仅为finance.startingEstimate,类似地为插值变量(放在{{}}内的任何内容)
答案 4 :(得分:0)
Use the below ..it will work... you need to define ng-app name and make change in your js controller...
<html ng-app ="My-app">
<body>
<form ng-controller="StartUpController">
Starting: <input ng-change="computeNeeded()" ng-model="funding.startingEstimate">
Recommendation: {{funding.needed}}
</form>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script>
var app= angular.module("My-app", []);
app.controller("StartUpController", ['$scope', function($scope) {
$scope.funding = { startingEstimate: 0, needed:1};
$scope.computeNeeded = function() {
$scope.funding.needed = $scope.funding.startingEstimate * 10;
};
}]);
</script>
</body>
</html>