AngularJS ng-bind与函数 [英] AngularJS ng-bind with a function

查看:177
本文介绍了AngularJS ng-bind与函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示附件"部分的表格格式.我有查询和结果数据.两者都有一个公共列attachmentTypeId.我想显示基于ID的附件类别描述.在ng-bind中,我尝试了一次,但没有成功.

I want to show a table format for the Attachments section. I have the lookup and results data. Both have a common column of attachmentTypeId. I want to show the attachment category description based on the id. In the ng-bind I tried an attempt, it's not worked.

我正在使用ng-bind中的函数,希望方法是错误的,请期待替代方法.

I am using a function in the ng-bind, hope the approach is wrong, expect an alternate for the approach.

attachmentLookup包含attachmentDescattachmentTypeId

  $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代码是

And the getCatgoryName code from the controller is,

$scope.getCatgoryName = function (attachmentTypeId) {
    angular.forEach($scope.attachmentLookup, function (attachemnt) {
        if (attachemnt.attachmentTypeId === attachmentTypeId)
            return attachemnt.attachmentDesc;
    });
};

样本柱塞: http://plnkr.co/edit/dZy5gW4q9CxWF2NszXYc

推荐答案

对括号进行了脏检查,因此将为每个$digest调用该函数.

The brackets are dirty checked, therefore the function will be called for every $digest.

ng-bind是指令,它将对传递给ng-bind的内容使用监视程序.

This ng-bind is a directive, it will use a watcher on what is being passed to ng-bind.

因此,ng-bind仅在传递的变量或值确实发生更改时才适用.

Therefore, ng-bind will only apply, when the passed variable or value does actually change.

使用函数,您没有传递变量,因此不会为每个$digest触发.

With a function, you are not passing a variable, therefore it will not fire for every $digest.

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

It is therefore, better to use brackets with a function call.

我在这里更新了监听器: http://plnkr.co/edit/LHC2IZ0Qk9LOOYsjrjaf?p =预览

I have updated the plunker here: http://plnkr.co/edit/LHC2IZ0Qk9LOOYsjrjaf?p=preview

我在这里更改了HTML:

I have changed the HTML here:

<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与函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆