添加指令使角度js代码中的控制器未定义

时间:2013-06-27 08:23:47

标签: javascript angularjs angularjs-directive directive

这是我的带有html代码的角度js应用程序

<html>
<head>
    <title></title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="Scripts/angular.min.js" type="text/javascript"></script>
    <script type="text/javascript" language="javascript">
        angular.module("demo", []).controller('DemoController', function ($scope) {
            $scope.user = {
                dateOfBirth: new Date(1970, 0, 1)
            }
        });
    </script>
</head>
<body>
    <div ng-app="demo" ng-controller="DemoController">
        Date Of Birth:
        <my-datepicker type="text" ng-model="user.dateOfBirth" />
        <br />
        Current user's date of birth: <span id="dateOfBirthDisplay">{{user.dateOfBirth}}</span>
    </div>
</body>
</html>

它运行正常。但是当我向它添加一个指令时,它显示错误

Error: Argument 'DemoController' is not a function, got undefined

以下是带指令的完整代码

<html>
<head>
    <title></title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="Scripts/angular.min.js" type="text/javascript"></script>
    <script type="text/javascript" language="javascript">
        angular.module("demo", []).controller('DemoController', function ($scope) {
            $scope.user = {
                dateOfBirth: new Date(1970, 0, 1)
            }
        });
        angular.module("demo", []).directive('myDatepicker', function ($parse) {
            return {
                restrict: "E",
                replace: true,
                transclude: false,
                compile: function (element, attrs) {
                    var modelAccessor = $parse(attrs.ngModel);

                    var html = "<input type='text' id='" + attrs.id + "' >" +
            "</input>";

                    var newElem = $(html);
                    element.replaceWith(newElem);

                    return function (scope, element, attrs, controller) {

                        var processChange = function () {
                            var date = new Date(element.datepicker("getDate"));

                            scope.$apply(function (scope) {
                                // Change bound variable
                                modelAccessor.assign(scope, date);
                            });
                        };

                        element.datepicker({
                            inline: true,
                            onClose: processChange,
                            onSelect: processChange
                        });

                        scope.$watch(modelAccessor, function (val) {
                            var date = new Date(val);
                            element.datepicker("setDate", date);
                        });

                    };

                }
            };
        });
    </script>
</head>
<body>
    <div ng-app="demo" ng-controller="DemoController">
        Date Of Birth:
        <my-datepicker type="text" ng-model="user.dateOfBirth" />
        <br />
        Current user's date of birth: <span id="dateOfBirthDisplay">{{user.dateOfBirth}}</span>
    </div>
</body>
</html>

我正在关注此链接中的教程http://henriquat.re/directives/advanced-directives-combining-angular-with-existing-components-and-jquery/angularAndJquery.html

1 个答案:

答案 0 :(得分:6)

您要定义demo模块两次。通过将第二个参数传递给angular.module method,您可以创建一个新模块。不要传递第二个参数以返回对现有模块的引用:

// Create a module
angular.module("demo", []).controller('DemoController', function ($scope) {
    // ...
});
// Get reference to module
angular.module("demo").directive('myDatepicker', function ($parse) {
    //              ^ Only one argument
});

请注意,controller方法返回允许链接的模块,module方法显然也返回模块,因此您还有两个其他选项:

链接:

angular.module("demo", []).controller('DemoController', function ($scope) {
    // ...
}).directive('myDatepicker', function ($parse) {
    // ...
});

存储对模块的引用:

var demo = angular.module("demo", []);
demo.controller('DemoController', function ($scope) {
    // ...
});
demo.directive('myDatepicker', function ($parse) {
    // ...
});