How to call a function multiple times from cell template in ui-grid in angularjs?

喜你入骨 提交于 2019-12-11 15:52:24

问题


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?

Here is my controller code:

 (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++;
        }


      });

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.


回答1:


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.

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('');


来源:https://stackoverflow.com/questions/53371278/how-to-call-a-function-multiple-times-from-cell-template-in-ui-grid-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!