【问题标题】:AngularJS ng-bind-html with a Variable in itAngularJS ng-bind-html 里面有一个变量
【发布时间】:2015-03-19 14:51:44
【问题描述】:

我是 AngularJS 的新手。 我有一个如下 HTML 表格:

<table>
<thead>
<tr>
    <th>ID</th>
    <th>other stuff</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="rule in rules">
    <td>@{{ rule.id }}</td>
    <td ng-init="functionThatExecutesHttpPost(rule.id)" ng-bind-html="variable"></td>
</tr>
</tbody>
</table>

Angular 函数如下所示:

$scope.functionThatExecutesHttpPost = function(ruleId) {
    $http.post('***'+ruleId).success(function(data) {
        $scope.variable = data;
    });
}

我现在遇到的问题是,我希望在我的 HTML 视图中拥有的数据在每个“td”中,而不仅仅是在具有正确 ID 的那个中。经过一番思考,这是您可以期待的行为。

我现在的问题是:
是否可以给'ng-bind-html'一个变量,如 --> variable(rule.id) 然后在 Angular 函数中我写一些类似 --> $scope.variable(ruleId) = data;
或者是否有解决这个问题的另一种方法?

提前致谢。

【问题讨论】:

  • 你为什么不ng-include then/

标签: angularjs ng-bind-html


【解决方案1】:

如果您不想执行任何操作,更好的解决方案是使用ng-include 指令加载模板。它将在 td 中加载所需的模板,就像您从 ajax 执行它一样。

标记

<tr ng-repeat="rule in rules">
    <td>@{{ rule.id }}</td>
    <td ng-include="'******'+ rule.id"></td>
</tr>

硬编码的 URL 保留在单引号中,其他动态 ng-repeat 当前变量通过与 + 符号连接来保持原样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 2015-09-04
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多