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

查看:90
本文介绍了如何在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.

有人可以帮我吗?

这是我的控制器代码:

 (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天全站免登陆