【问题标题】:Function assigning to ng-model in Angular在Angular中分配给ng-model的函数
【发布时间】:2014-10-15 15:31:17
【问题描述】:

我想将滑块集成到我的网站中。我为此使用http://darul75.github.io/ng-slider/。但是我不想为我的滑块使用硬编码的起点和终点。我也不想要一个硬编码的默认值,例如

  $scope.value = 1000000;
  $scope.options={
      from :10,
      to : 100,
      step : 1,
      dimension : " $"
  }

在我看来

<input ng-model="value"  type="text" id="mySlider1" slider options="options" />

相反,我更喜欢对 options.from/options.to 和 value 使用一个函数。

例如 value 的函数应该是这样的:

function(){
          var arrayToPush=[];
          for(var i=0;i<$scope.data.length;i++){
              arrayToPush.push($scope.data[i].hops[0].total_price.EUR)
          }
          return arrayToPush.max();
      };

请注意,数据来自标准 HTTP 请求并返回一个数组 fromto的函数应该类似。首先,如何将函数分配给 ng-model,其次如何将函数分配给from

希望你能帮我解决这个问题

【问题讨论】:

    标签: angularjs angularjs-scope angular-ngmodel angularjs-ng-model


    【解决方案1】:

    刚刚找到答案。我定义了一个附加函数:

    $scope.maxValue=(function(){
              var arrayToPush=[];
              for(var i=0;i<$scope.data.length;i++){
                  arrayToPush.push($scope.data[i].hops[0].total_price.EUR)
              }
              return arrayToPush.length>0 ? Math.round(Math.max.apply( Math, arrayToPush)) : 5000;
          })($scope.data); 
    

    并将其分配给valueto。但是,错误的部分是我的函数获取数组的最大值,第二个函数分配一个函数,而不仅仅是值。

    【讨论】:

      猜你喜欢
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-24
      • 2016-04-27
      • 2017-04-02
      • 2012-12-20
      • 1970-01-01
      相关资源
      最近更新 更多