AngularJS:参数“Ctrl”不是一个函数,没有定义

AngularJS: Argument 'Ctrl' is not a function, got undefined

本文关键字:一个 函数 定义 参数 Ctrl AngularJS      更新时间:2023-09-26

我正在学习AngularJS,在实现控制器时遇到了这个错误。

有人可以指出出了什么问题吗?(完全按照教程中显示的方式进行操作,除非某些函数已弃用?

我收到以下错误: Argument 'Ctrl' is not a function, got undefined

.HTML

<!DOCTYPE html>
<html ng-app>
<head lang="en">
<meta charset="UTF-8">
<title>AngularJS Controller</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js">   </script>
</head>
<body>
<div ng-controller="Ctrl">
    <input ng-model="name">
    <input ng-model="age">
    <h1>{{ name }}</h1>
    <h1>{{ age }}</h1>
</div>
<script>
    var Ctrl = function($scope) {
        $scope.name = "Noob";
        $scope.age = "21";
    };
</script>

据我所知,您需要使用module.controller方法定义控制器。例如,将应用命名为 myApp

<html ng-app="myApp">

JS部分将是:

angular.module('myApp', [])
    .controller('Ctrl', ['$scope', function($scope) {
        $scope.name = "Noob";
        $scope.age = "21";
    }]);

您需要定义您的应用程序

var myApp = angular.module('myApp',[]);

并将$scope传递到控制器中

var Ctrl = function($scope) {

以下是这些更改的小提琴链接:http://jsfiddle.net/fxk7mtb7/