简体   繁体   English

ng-model的值在自定义指令中未定义

[英]Value of ng-model undefined in custom directive

I am writing a custom directive which calls a legacy javascript date control we are using in other places.. The issue I am facing is the value of the ng-model I send in is always undefined. 我正在编写一个自定义指令,该指令调用我们在其他地方使用的旧版javascript日期控件。我面临的问题是我发送的ng-model的值始终是不确定的。

This value is coming from a $http.get which is executed in the controller. 该值来自在控制器中执行的$http.get The code for the directive seems to be hit before the controller code is run which is why this is happening. 该指令的代码似乎在运行控制器代码之前就已命中,这就是这种情况发生的原因。 Is there a way for me to tell the directive only to run after the controller has been initialized? 我有办法告诉指令仅在控制器初始化后才能运行吗?

app.directive('dateControl', function () {
    return {
        restrict: 'A',
        require: '^ngModel',
        scope: {
            ngModel: '='
        },
        link: function (scope, element, attr) {
            // scope.ngModel is undefined in here
            alert(scope.ngModel);
            addCalControlAngular(attr.id, scope.ngModel, attr.ngModel, attr.id);
        }
    }
});


<td date-Control ng-model="postAward.letterOfAwardDespatchDate" id="letterofaward">

The directive is not 'hit' before controller if it's inside controller, but if the value comes from http request then it's undefined until the model is updated with value (hope that makes sense), to assure that directive is executed after receiving data from http you can do a small trick with ng-if 如果指令位于控制器内部,则不会在控制器之前“命中”,但是如果值来自http请求,则在定义模型更新值之前(在有意义的情况下)它是不确定的,以确保在从http接收数据后执行指令你可以用ng-if做个小把戏

<td procon-Date-Control ng-if="postAward.letterOfAwardDespatchDate" ng-model="postAward.letterOfAwardDespatchDate" id="letterofaward">

If you do it that way directive is going to be rendered in view only of the ng-if is not null/undefined or the expression is truthy ie ng-if="postAward.letterOfAwardDespatchDate == 'yesterday'" 如果这样做,将仅在ng-if不为null / undefined或表达式为真的情况下呈现指令,即ng-if =“ postAward.letterOfAwardDespatchDate =='yesterday'”

I resolved this with a $watch in my link function 我用链接功能中的$ watch解决了这个问题

link: function (scope, element, attr) {
            debugger;
            // Watch for a value in ngModel and render that value, otherwise give a default
            scope.$watch('ngModel', function (newVal) {
                if (newVal) {
                    addCalControlAngular(attr.id, scope.ngModel, attr.isMandatory, attr.showDefault, attr.ngModel, attr.id);
                } else {
                    addCalControlAngular(attr.id, c_NULL_DATE, attr.isMandatory, attr.showDefault, attr.ngModel, attr.id);
                }
            })            
        }

If you want to use ngModel try to add a argument in link function to add a injection like this: 如果要使用ngModel尝试在链接函数中添加一个参数以添加如下所示的注入:

link: function (scope, element, attr, ngModel)

After this, you can use code like ngModel.$render and so on, hope that helpful to you. 之后,您可以使用ngModel.$render类的代码,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM