Javascript 角度';s绑定不';不能使用指令中添加的元素

Javascript 角度';s绑定不';不能使用指令中添加的元素,javascript,angularjs,Javascript,Angularjs,我的指示是 myApp.directive('myRequired', function ($compile, gettextCatalog) { return { link: function (scope, element, attrs) { var noticeContainer = '<div class="inputError" ng-show="showSomeNotices" translate>{{errorMessag

我的指示是

myApp.directive('myRequired', function ($compile, gettextCatalog) {
    return {
        link: function (scope, element, attrs) {
            var noticeContainer = '<div class="inputError" ng-show="showSomeNotices" translate>{{errorMessage}}</div>';
            element.after(noticeContainer);

        scope.$on("submitstart", function (event, data) {
            scope.showSomeNotices = false;
            if (!element.val()) {
                scope.errorMessage = gettextCatalog.getString("Empty field");                
                scope.showSomeNotices = true;

            }
        });
    }};
});
myApp.directive('myRequired',函数($compile,gettextCatalog){
返回{
链接:函数(范围、元素、属性){
var noticeContainer='{{errorMessage}}';
元素之后(noticeContainer);
作用域:$on(“submitstart”,函数(事件、数据){
scope.showSomeNotices=false;
如果(!element.val()){
scope.errorMessage=gettextCatalog.getString(“空字段”);
scope.showSomeNotices=true;
}
});
}};
});

但是ng show和大括号
{{errorMessage}
忽略连接的变量。我总是在我的页面上看到
{{errorMessage}}
作为文本。如何修复?

在上面的注释中提到,您必须像这样编译字符串:

…
var noticeContainer = '<div class="inputError" ng-show="showSomeNotices" translate>{{errorMessage}}</div>',
    content = $compile(noticeContainer)(scope);

element.after(content);
…
…
var noticeContainer='{{errorMessage}}',
内容=$compile(noticeContainer)(范围);
元素。之后(内容);
…

我已经为您创建了一个plunkr:

您必须编译
noticeContainer
(使用$compile)div以
ng show
/
任何角度绑定才能在该DOMUse上启用$compile-inside-link函数。@PankajParkar,您能告诉我具体要做什么吗?因为
$complie(noticeContainer)(scope)
不起作用。errorMessage&showSomeNotices作用域变量属于您正在编译的同一作用域?@PankajParkar,是的。我可以在调试器中看到我在$compile调用中使用的范围变量中的errorMessage和showSomeNotices。@splash27我编辑了我的答案,只做了一点修改,但我不知道我的代码出了什么问题。但当我从plunkr复制并粘贴您的代码时,它开始工作了。非常感谢。