我对角度js很新,以下是我的代码
<script type="text/javascript" src="node_modules/angular/angular.min.js"></script>
<script type="text/javascript">
(function(angular){
var testAngular = angular.module('testAngular');
testAngular.controller = ("name_controller", function($scope) {console.log("hello");
$scope.name = {
firstName: "null",
lastName: "null",
setName: function(fname, lname) {
if(fname.trim != "") {
this.firstName = fname;
}
if(lname.trim()!="") {
this.lastName = lname;
}
},
getName: function() {
var name_object = $scope.name;
return name_object.firstName+" "+name_object.lastName;
}
};
});
})(window.angular);
</script>
<div ng-app="testAngular" ng-controller="name_controller">
Enter first name: <input type="text" ng-model="name.firstName"><br><br>
Enter last name: <input type="text" ng-model="name.lastName"><br>
<br>
You are entering: {{ name.firstName }}
</div>
现在,当我尝试运行此代码时,我在控制台中遇到了2个错误
[$injector:nomod]
和
[$injector:modulerr]
知道为什么会发生这种情况。有些帖子说我需要包含路由模块,但我没有在我的代码中使用路由。
答案 0 :(得分:1)
尝试改变:
testAngular.controller = ("name_controller", function($scope) {
到
testAngular.controller("name_controller", function($scope) {