无法编写基本控制器 AngularJS | 无法编写基本控制器
本文介绍了无法编写基本控制器 AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述 如果我的问题太基础,请原谅我.我正在学习 AngularJS 的控制器.
这是项目结构.
这是我的 html:
<头><script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script><script src="js/controllers.js"/>头部><身体><div ng-controller="numberController"><input type = "text" ng-model = "number.number1" placeholder = "在此处输入数字"><input type = "text" ng-model = "number.number2" placeholder = "在此处输入另一个数字">{{number.number1}} + {{number.number2}} = {{number.sumIt()}}
</html>
这是我的控制器:
function numberController($scope) {$scope.number = {编号 1:0,数字2:0,sumIt:函数(){var numberObject;numberObject = $scope.number;返回 parseInt(numberObject.number1) + parseInt(numberObject.number2);}};}
网页甚至无法加载.
我哪里出错了?
我得到的错误:我得到这个错误:https://docs.angularjs.org/error/ $injector/modulerr?p0=sa-app&p1=Error:%20%5B$injector:nomod%5D%20http:%2F%2Ferrors.angularjs.org%2F1.3.3%2F$injector%2Fnomod%3Fp0%3Dsa-app%0A%20%20%20%20at%20Error%20(native)%0A%20%20%20%20at%20https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:6:416%0A%20%20%20%20at%20https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:21:366%0A%20%20%20%20at%20a%20(https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:21:7)%0A%20%20%20%20at%20https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:21:250%0A%20%20%20%20at%20https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:35:105%0A%20%20%20%20at%20r%20(https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:7:302)%0A%20%20%20%20at%20g%20(https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:34:456)%0A%20%20%20%20at%20Lb%20(https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:38:184)%0A%20%20%20%20at%20d%20(https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:17:381
我更新的控制器:
var saApp = angular.module("saApp", []);saApp.controller('numberController', function($scope) {$scope.number = {编号 1:0,数字2:0,sumIt:函数(){var numberObject;numberObject = $scope.number;返回 parseInt(numberObject.number1) + parseInt(numberObject.number2);}};});
我更新的 HTML:
<头><script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script><script src="js/controllers.js"/>头部><身体><div ng-app = "saApp" ng-controller="numberController"><input type = "text" ng-model = "number.number1" placeholder = "在此处输入数字"><input type = "text" ng-model = "number.number2" placeholder = "在此处输入另一个数字">{{number.number1}} + {{number.number2}} = {{number.sumIt()}}</html>
解决方案
您需要先将您的应用程序与您的 JS 文件链接起来.将您的标签更改为并更改您的controllers.js
如果您右键单击鼠标然后单击检查元素,也会找到控制台.
Forgive me if my question is too basic. I am learning AngularJS's controllers.
This is the project structure.
This is my html:
<!doctype html>
<html ng-app>
<head>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
<script src="js/controllers.js" />
</head>
<body>
<div ng-controller="numberController">
<input type = "text" ng-model = "number.number1" placeholder = "Enter a number here">
<input type = "text" ng-model = "number.number2" placeholder = "Enter another number here">
{{number.number1}} + {{number.number2}} = {{number.sumIt()}}
</div>
</body>
</html>
This is my controller:
function numberController($scope) {
$scope.number = {
number1: 0,
number2: 0,
sumIt: function() {
var numberObject;
numberObject = $scope.number;
return parseInt(numberObject.number1) + parseInt(numberObject.number2);
}
};
}
The web page doesn't even load.
Where have I gone wrong?
Edit:
The error that I get: I get this error: https://docs.angularjs.org/error/ $injector/modulerr?p0=sa-app&p1=Error:%20%5B$injector:nomod%5D%20http:%2F%2Ferrors.angularjs.org%2F1.3.3%2F$injector%2Fnomod%3Fp0%3Dsa-app%0A%20%20%20%20at%20Error%20(native)%0A%20%20%20%20at%20https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:6:416%0A%20%20%20%20at%20https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:21:366%0A%20%20%20%20at%20a%20(https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:21:7)%0A%20%20%20%20at%20https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:21:250%0A%20%20%20%20at%20https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:35:105%0A%20%20%20%20at%20r%20(https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:7:302)%0A%20%20%20%20at%20g%20(https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:34:456)%0A%20%20%20%20at%20Lb%20(https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:38:184)%0A%20%20%20%20at%20d%20(https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:17:381
My updated controller:
var saApp = angular.module("saApp", []);
saApp.controller('numberController', function($scope) {
$scope.number = {
number1: 0,
number2: 0,
sumIt: function() {
var numberObject;
numberObject = $scope.number;
return parseInt(numberObject.number1) + parseInt(numberObject.number2);
}
};
});
My updated HTML:
<!doctype html>
<html>
<head>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
<script src="js/controllers.js" />
</head>
<body>
<div ng-app = "saApp" ng-controller="numberController">
<input type = "text" ng-model = "number.number1" placeholder = "Enter a number here">
<input type = "text" ng-model = "number.number2" placeholder = "Enter another number here">
{{number.number1}} + {{number.number2}} = {{number.sumIt()}}
</div>
</body>
</html>
解决方案
You need to link your application with your JS file first.Change your tag toAnd change your controllers.js
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.number = {
number1: 0,
number2: 0,
sumIt: function() {
var numberObject;
numberObject = $scope.number;
return parseInt(numberObject.number1) + parseInt(numberObject.number2);
}
};
});
</script>
Also the console is found if you right click on your mouse then click on Inspect element.
这篇关于无法编写基本控制器 AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-05 22:24