Angular Function not Called in <span> tag

Angular Function not Called in <span> tag

本文关键字:span gt tag Function lt Called in Angular not      更新时间:2023-09-26

我加载了一个数据表。但是我需要特定列上的数据是由函数生成的。这个函数没有被调用。我不知道为什么

vm.itemName = '';
vm.getItemName = function (item) {
    termService.getItemName({ itemId: term.itemID })
     .then(function (result) {
           vm.itemName = JSON.stringify(result.data);
    });
};

page.cshtml

<td>
    <span>{{vm.itemName }}</span>
</td>

呈现页面时,列为空。当我调试应用程序时,我意识到该函数没有被调用。

N。B函数通过web API调用c#方法。

编辑

我使用下面的函数得到itemsList,它工作得很好。

vm.terms = [];
function getTermsList() {
    termService.getTermsList({}).success(function (result) {
       vm.terms = result.items;
    });
}

我的表格网格是这样的这样你就知道item来自哪里了

<tbody>
    <tr ng-repeat="item in vm.terms">
        <td>....</td>
    </tr>
</tbody>

"getter"并不像你使用SERVER端渲染时那样神奇地被调用,但我们可以让它像你期望的那样工作:

vm._itemName = '';
var itemNamePromise = null;
//define itemName getter
Object.defineProperty(vm, "itemName", { 
  get: function() {
    itemNamePromise = if(itemName === '' && itemNamePromise === null){ //to avoid multile service calls
      termService.getItemName({ itemId: term.itemID })
       .then(function (result) {
           vm._itemName = JSON.stringify(result.data);
      });
    }
    return vm._itemName;
  }
}); 

参见参考资料中的兼容性:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/get