未知提供者:ngModelProvider

时间:2014-09-03 12:34:42

标签: javascript angularjs directive

我正在尝试在Angular指令中注入ngModel,我收到此错误:

Error: [$injector:unpr] Unknown provider: ngModelProvider <- ngModel
http://errors.angularjs.org/1.2.18/$injector/unpr?p0=ngModelProvider%20%3C-%20ngModel
    at http://localhost:2013/Scripts/angular.js:78:12
    at http://localhost:2013/Scripts/angular.js:3741:19
    at Object.getService [as get] (http://localhost:2013/Scripts/angular.js:3869:39)
    at http://localhost:2013/Scripts/angular.js:3746:45
    at getService (http://localhost:2013/Scripts/angular.js:3869:39)
    at invoke (http://localhost:2013/Scripts/angular.js:3896:13)
    at Object.instantiate (http://localhost:2013/Scripts/angular.js:3917:23)
    at $get (http://localhost:2013/Scripts/angular.js:7201:28)
    at http://localhost:2013/Scripts/angular.js:6592:34
    at forEach (http://localhost:2013/Scripts/angular.js:327:20) angular.js:9937
(anonymous function) angular.js:9937
$get angular.js:7283
$get.Scope.$digest angular.js:12414
$get.Scope.$apply angular.js:12660
done angular.js:8272
completeRequest angular.js:8477
xhr.onreadystatechange

这是我的指示:

module.directive("myDatePicker", function () {
    return {
        restrict: "A",
        template: '<p class="input-group" title="{{title}}">' +
                            '<input type="text" class="form-control" data-datepicker-popup="{{dateFormat}}" ng-model="selectedDate" data-is-open="isOpen" data-datepicker-options="dateOptions" ng-required="true" data-close-text="{{closeText}}" />' +
                            '<span class="input-group-btn">' +
                                '<button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>' +
                            '</span>' +
                        '</p>',
        replace: true,
        require: '?ngModel',
        //priority: 0,
        scope: {
            title: "@",
            selectedDate: "=ngModel",
            onChange: "&",
            dateFormat: "="
        },
        compile: function (tElement, tAttrs, transclude) {
            // Correct ngModel for isolate scope
            if (tAttrs.ngModel) {
                tAttrs.$set('selectedDate', tAttrs.ngModel, false);
                tAttrs.$set('ngModel', 'selectedDate', false);
            }

            return {
                post: function (scope, iElement, iAttrs, controller) {
                    // Render
                    return controller.$render = function () {
                        if (!controller.$viewValue) {
                            return;
                        }
                        angular.extend(scope, controller.$viewValue);
                    };
                }
            };
        },
        priority: 100,
        link: function (scope, elem, attr) {
        },
        controller: function ($scope, global, ngModel) {
            $scope.isOpen = false;
            $scope.closeText = 'Close';
            $scope.dateOptions = {};

            $scope.open = function ($event) {
                $event.preventDefault();
                $event.stopPropagation();
                $scope.isOpen = true;
            };

            $scope.$watch(function () {
                return $scope.selectedDate;
            },
                function (newValue, oldValue) {
                    ngModel.$setValidity('required', newValue == true);
                    $scope.onChange({ newDate: newValue, oldDate: oldValue });
                }, true);
        }
    };
});

这是我的HTML:

                    <input data-my-date-picker
                           id="EventDatePicker" data-name="EventDatePicker"
                           data-date-format="dateFormat"
                            ng-model="EventDetails.Date"
                           ng-required="true"
                           data-title="Date" />

为什么无法解决? 我试过这样做:

module.directive("myDatePicker", ['ngModel', function () {

......但它没有帮助。

我也尝试删除priority,但它也没有帮助。

是否与指令加载的顺序有关?

我在这里缺少什么?

有什么想法吗?


根据sma的建议,最后我做到了这一点:

        require: ['^ngModel'/*, '^isDate'*/],
        scope: {
            title: "@?",
            name: "@?"
        },
        link: function (scope, elem, attr, ngModel/*, isDate*/) {
        ...

这种似乎有用。

我不知道为什么我不能在控制器中注入它。 :(

一定有办法,但我还没找到......

但由于一些奇怪的原因,上面的isDate之类的指令仍未被注入(尽管它们是在当前指令之前定义的)。

这令人困惑。

1 个答案:

答案 0 :(得分:5)

您的控制器功能中的参数错误。控制器功能接受:

controller : function ($scope, $element) {
   ...    
}

如果您尝试将ngModel作为依赖项注入,请改用链接函数:

link : function(scope, element, attrs, ngModelController) {
   ...
}
相关问题