1

我正在尝试创建一个使用包含属性和验证等的模式对象生成的 ui。因此,我需要使用指令在我的 ui 控件上设置 ngModel。ngModel 的值是一个字符串,它表示范围内模式对象的属性路径。

我有这个适用于标准输入,但是在使用 angular ui datepicker 时出现以下错误。

Error: [$compile:ctreq] Controller 'ngModel', required by directive 'myModel', can't be found!
http://errors.angularjs.org/1.2.10/$compile/ctreq?p0=ngModel&p1=myModel
at http://localhost:3000/javascripts/angular.js:78:20
at getControllers (http://localhost:3000/javascripts/angular.js:6054:39)
at nodeLinkFn (http://localhost:3000/javascripts/angular.js:6225:55)
at compositeLinkFn (http://localhost:3000/javascripts/angular.js:5634:37)
at compositeLinkFn (http://localhost:3000/javascripts/angular.js:5637:33)
at compositeLinkFn (http://localhost:3000/javascripts/angular.js:5637:33)
at publicLinkFn (http://localhost:3000/javascripts/angular.js:5539:46)
at boundTranscludeFn (http://localhost:3000/javascripts/angular.js:5653:37)
at controllersBoundTransclude (http://localhost:3000/javascripts/angular.js:6245:36)
at Object.ngIfWatchAction [as fn] (http://localhost:3000/javascripts/angular.js:18316:29)

<input class="form-control" datepicker-popup="dd-MMM-yyyy" my-model="" is open="property.calOpen" close-text="Close" ng-model="editModel.Person.Detail.DateOfBirth">

我的指令如下。

angular.module('MyDirectives',[])
.directive('myModel', function($compile , $timeout) {
    return {
        restrict: 'A',                       
        priority:0,

        link: function(scope,element, attr) {

            if(angular.isDefined(attr.ngModel))return;
            var field = scope.path ? scope.path + '.' + scope.key : scope.key;
            attr.$set("ngModel", "editModel." + field);

            console.log("in directive");
            $timeout(function(){
                $compile(element)(scope);
            });
        }
    };

由于 ngModel 的价值存在于范围内,我相信我需要链接功能而不是编译。我已经尝试添加Require: ?ngModel到没有区别的指令中。还尝试增加优先级,但这会将错误更改为 Error: [$compile:ctreq] Controller 'ngModel', required by directive 'input', can't be found!

如果我删除$timeout(function(){}周围的$compile(element)(scope)2 个弹出日历,则会出现重叠。这在浏览数月时很明显。

有任何想法吗

更新:见链接 plkr

4

1 回答 1

1

您需要创建一个新的双向绑定到本地范围变量(范围:true)。
使用 $parse 在路径中定位属性。

查看更新的 Plunker

于 2014-03-21T12:30:59.747 回答