如何从angularjs的ui-grid中的单元格模板多次调用函数? [英] How to call a function multiple times from cell template in ui-grid in angularjs?

查看:22
本文介绍了如何从angularjs的ui-grid中的单元格模板多次调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 angularJs(1.6) 中有一个使用 ui-grid 的控制器.问题是只有一个单元格模板.我想在此模板中获得不同的评级百分比值.我创建了一个函数getRating()"并调用了它.但它只会被调用一次.因此,我为所有模板获得了相同的值.

I have one controller in angularJs(1.6) which is using ui-grid. Problem is there is one cell template. I want to get different values for rating percentage in this template. I have created a function "getRating()" and called it. But it gets called only one time. Hence I get the same value for all the template.

有人能帮我解决这个问题吗?

Could anyone please help me with this?

这是我的控制器代码:

 (function () {
    'use strict';

     define([
         'angular'
     ], function (angular) {

     function SelectToolController($scope, $timeout, $filter, uiGridConstants) {
                  var vm = this,
                      _gridApi,
                      _cellTemplate,
                      _columnDefs,
                      _starRatingTemplate,
                      _starRatingColumn,
                      _starEnable;
            };
            _starRatingTemplate = [
                  '<div class="opr-star-rating"  >',
                  '<opr-star-rating rating-percentage="'+getRating()+'">',
                  '</opr-star-rating>',
                  '</div>'
            ].join('');

      vm.rating = 10;

      //this func is called from _starRatingTemplate 
       vm.getRating = function(){
           return vm.rating++;
        }


      });

如您所见,每次调用 getRating 函数时,我都会增加该值.但是这个函数只被调用一次.所以我最终得到了网格中所有单元格的值 11.

As you can see I am incrementing the value everytime the function getRating is called. But this function is called only one time. So I end up getting value 11 for all the cells in my grid.

推荐答案

模板字符串被计算"一次并在每个单元格中使用,因此对 getRating() 的调用只进行一次,构建模板字符串时.

The template string is "calculated" once and used in each cell, so the call to getRating() is made only once, when building the template string.

要每次调用该函数,您应该使用mustache"{{}} 并引用您的控制器.

To make a call to the function each time, you should use the 'mustache' {{}} and reference your controller.

所以你的代码应该类似于:

So your code should be similar to this:

_starRatingTemplate = [
    '<div class="opr-star-rating"  >',
    '<opr-star-rating rating-percentage="{{$ctrl.getRating()}}">',
    '</opr-star-rating>',
    '</div>'
].join('');

这篇关于如何从angularjs的ui-grid中的单元格模板多次调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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