如何在指令模板中呈现双花括号?

How to render double curly braces in directive template?

我正在与 AngularJS 进行后端验证集成。我需要在控件旁边显示一条错误消息。错误消息来自后端。控件由指令模板呈现。我尝试将错误消息的跨度与模板中的控件一起放置,但我 运行 陷入了尴尬的境地。

代码如下:

var myDirective = angular.module('myDirective', []);
myDirective.directive("textquestion", function() {
    return {
        template: '<input id="{{questionNumber}}" name="{{questionNumber}}" type="text" /><span ng-show="errors[{{questionNumber}}]">{{error[questionNumber]}}</span>',
        restrict: 'A',
        scope: {
            questionNumber: 'questionNumber'
        }
    };
})

如您所见,我想在html中渲染{{errors['1001']}},其中'1001'是实际的questionNumber,因此可以绑定错误消息。但是在目前的代码中,由于errors['1001']在渲染时是空的,所以这部分在html中什么也不会渲染。

任何人都可以帮助我如何在 html 中呈现实际的 {{errors['1001']}} 吗?还是有更好的方法来做到这一点?请记住,控件是动态生成的,因此 questionNumber 需要是动态的。而且这个错误信息来自后端,所以绑定需要在渲染之后发生。

谢谢!

您的 ng-show 中不需要 {{}}。你传递给 ng-show 的已经是一个表达式

<span ng-show="errors[questionNumber]">{{errors[questionNumber]}}</span>