带有函数的 AngularJS ng-bind

标签 angularjs angularjs-ng-repeat ng-bind

我想显示附件部分的表格格式。我有查找和结果数据。两者都有一个共同的列 attachmentTypeId .我想根据 id 显示附件类别描述。在 ng-bind我试过了,没用。

我正在使用 ng-bind 中的一个函数,希望方法是错误的,期待替代方法。
attachmentLookup包含 attachmentDesc , attachmentTypeId

  $scope.attachmentLookup = [{
    "attachmentDesc": "Category One",
    "attachmentTypeId": "1"
  }, {
    "attachmentDesc": "Category Two",
    "attachmentTypeId": "2"
  }, {
    "attachmentDesc": "Category Three",
    "attachmentTypeId": "3"
  }, {
    "attachmentDesc": "Category Four",
    "attachmentTypeId": "4"
  }, {
    "attachmentDesc": "Category Five",
    "attachmentTypeId": "5"
  }];

attachmentDetails来自数据库的数据为,
  $scope.attachmentDetails = [{
    "attachmentId": "001",
    "fileName": "File Name 001",
    "attachmentTypeId": "1"
  }, {
    "attachmentId": "003",
    "fileName": "File Name 003",
    "attachmentTypeId": "2"
  }, {
    "attachmentId": "005",
    "fileName": "File Name 005",
    "attachmentTypeId": "3"
  }, {
    "attachmentId": "007",
    "fileName": "File Name 007",
    "attachmentTypeId": "1"
  }, {
    "attachmentId": "009",
    "fileName": "File Name 009",
    "attachmentTypeId": "2"
  }, {
    "attachmentId": "011",
    "fileName": "File Name 011",
    "attachmentTypeId": "3"
  }];

HTML代码为,
<table>
  <thead>
    <tr>
      <th>File Name</th>
      <th>|</th>
      <th>Category</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="attachment in attachmentDetails">
      <td> <span ng-bind="attachment.fileName"></span>
      </td>
      <td>|</td>
      <td> <span ng-bind="getCatgoryName(attachment.attachmentTypeId)"></span>
      </td>
    </tr>
  </tbody>
</table>

getCatgoryName来自 Controller 的代码是,
$scope.getCatgoryName = function (attachmentTypeId) {
    angular.forEach($scope.attachmentLookup, function (attachemnt) {
        if (attachemnt.attachmentTypeId === attachmentTypeId)
            return attachemnt.attachmentDesc;
    });
};

sample Plunker:http://plnkr.co/edit/dZy5gW4q9CxWF2NszXYc

最佳答案

括号是脏检查的,因此每个 $digest 都会调用该函数。 .

ng-bind是一个指令,它将在传递给 ng-bind 的内容上使用观察者.

因此,ng-bind仅当传递的变量或值确实发生变化时才适用。

对于函数,您不会传递变量,因此它不会为每个 $digest 触发。 .

因此,最好在函数调用中使用方括号。

我在这里更新了 plunker:http://plnkr.co/edit/LHC2IZ0Qk9LOOYsjrjaf?p=preview

我在这里更改了 HTML:

<tr ng-repeat="a in attachmentDetails">
    <td> <span>{{a.fileName}}</span></td>
    <td>|</td>
    <td> {{ getCatgoryName(a.attachmentTypeId) }}</td>
</tr>

函数也做了修改:
  $scope.getCatgoryName = function(attachmentTypeId) {
    var desc = "";
    angular.forEach($scope.attachmentLookup, function(attachemnt) {
      if (parseInt(attachemnt.attachmentTypeId) == attachmentTypeId)
        desc = attachemnt.attachmentDesc;
    });

    return desc;
  };

关于带有函数的 AngularJS ng-bind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30437563/

相关文章:

angularjs - 在 Angular 中修改数组不起作用

javascript - 如何通过调用 is-open 属性中的函数来保留组中最后打开的 Accordion

javascript - 如何防止 Handlebars "{{}}"在实际数据加载之前出现在 AngularJs 应用程序的 UI 中?

AngularJS - 为什么 ng-bind 比表达式更快?

javascript - 使用远程数据源的 Angular Material Design 自动完成及其性能与最佳方法

javascript - AngularJS 中的场景测试

javascript - Angularjs 数据表插件不会第二次绑定(bind)

javascript - Angular Jasmine UI 路由器将解析值注入(inject)测试

javascript - ng-repeat order通过保持固定的最高值

javascript - 如何在 ng-bind-html 中呈现 Angular 变量的内容