angularjs 不识别function ,望指教
发布于 21天前 作者 qianjiahao 245 次浏览 来自 问答

代码:

       <!doctype html>
        <html>
        <head>
       <link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.css">
       <script src="../node_modules/angular/angular.js"></script>
        </head>
        <body>
            <div ng-app ng-controller="HelloCtrl">
                        Your name: <input type="text" ng-model="name" placeholder="World">
                        <hr>
                           Hello {{name}}!
            </div>
        <script>
            var HelloCtrl = function($scope){
            $scope.name = 'world';
            }
        </script>
        </body>
        </html>

一个angular的例子,但是总是报错:

    Error: [ng:areq] Argument 'HelloCtrl' is not a function, got undefined
    http://errors.angularjs.org/1.3.15/ng/areq?p0=HelloCtrl&p1=not%20a%20function%2C%20got%20undefined
   at REGEX_STRING_REGEXP (angular.js:63)
    at assertArg (angular.js:1587)  
    at assertArgFn (angular.js:1597)
    at angular.js:8470
    at angular.js:7638
    at forEach (angular.js:331)
    at nodeLinkFn (angular.js:7625)
    at compositeLinkFn (angular.js:7117)
    at publicLinkFn (angular.js:6996)
    at angular.js:1457

不知道是为何~~?

6 回复

你controller里面写错了

@jintaolee 应该咋写啊。。。

我想应该这样写,

html 标签属性:ng-app="demo" 
script脚本:
angular.module("demo",[]).controller("HelloCtrl",['$scope',function($scope){
   $scope.name = 'world';
}])

新 的版本不支持全局函数的controller

@fjcgreat 谢谢,我发现是的我的module方法不对,才不识别的。

回到顶部