0

我正在尝试在指令中使用多个 ng-bind 而不是 {{}} ,但没有运气。

<my-directive att1="age" att2="22"></my-directive>

angular
    .module("app",[])
    .directive('myDirective', myDirective);

function myDirective() {
    return {
        restrict: 'E',
        scope: {
            att1: '@',
            att2: '@'
        },
        template: '<div class="ng-bind: att1;" ng-bind="att2"></div>'
    }
}

万一只有一个 ng-bind 也没问题。如果我有不止一个,就像在这种情况下,我在某处读到了我正在使用的符号,"ng-bind: att;"但它不起作用。

另外,我想知道我是否真的需要它。显然使用 ng-bind 避免了双括号在加载数据之前在屏幕中闪烁的问题,在这种情况下,这并不重要,因为我将值用作类属性,因此不会显示在屏幕中. 另一方面,使用 ng-bind 应该更有效。

有任何想法吗?我在stackoverflow中看到了一些关于此的问题,但似乎都没有解决这个问题。

4

1 回答 1

1

ng-bind每个属性只能有一个。的值ng-bind被写入div标签的内容。当您有多个ng-binds 时,它们会相互覆盖,因此这是不可能的。

但是,如果您想设置一个类,请查看ngClass此处的指令https://docs.angularjs.org/api/ng/directive/ngClass。您的模板可能看起来像这样:

template: '<div ng-class="att1" ng-bind="att2"></div>'

这会将 的内容设置为 youratt1class="..."属性,div并将 的内容设置att2为 your 的内容div。渲染的输出可能是

<div class="your-att1-value">your att2 value</div>

如果你想要多个,ng-bind你可以为每个 ng-bind 插入一个单独的标签,如下所示:

template: '<div><span ng-bind="att1"></span><span ng-bind="att2"></span></div>'
于 2015-06-16T07:27:37.790 回答