角度:'也许'数字格式化程序

时间:2016-12-12 13:46:37

标签: angularjs filter numbers

是否可以装饰angular number filter,如果输入不是数字则返回未修改的输入?

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button

dataX = np.array([1,2,3,4,5,6,7,8,9,10])
dataY = np.array([1193,1225,1125,1644,1255,13676,2007,2008,12359,1210])

ax = plt.subplot(111)
def on_click(event):
    if event.dblclick:
       ax.plot((event.xdata, event.xdata), (mean-standardDeviation, mean+standardDeviation), 'r-')
       plt.show()

def on_enter(event):
    axcut.set_visible(True)
def on_leave(event):
    axcut.set_visible(False)
def _yes(event):
    print("yolo")

mean = np.mean(dataY)
standardDeviation = np.std(dataY)

ax.plot(dataX, dataY, linewidth=0.5)
plt.connect('button_press_event', on_click)
plt.connect("axes_enter_event", on_enter)
plt.connect("axes_leave_event", on_leave)


axcut = plt.axes([0.9, 0.0, 0.1, 0.075])
bcut = Button(axcut, 'YES', color='red', hovercolor='green')
axcut.set_visible(False)
bcut.on_clicked(_yes)

plt.show()

<my-tag input-value="{{foo() | number: 2}}"></my-tag> 返回1.2345时,该值应显示为1.23,但是当它返回&#34; abcd&#34;它只会返回:&#34; abcd&#34;。

1 个答案:

答案 0 :(得分:0)

如果您想使用number过滤器进行库存,您可以按照@Ankh链接的建议构建一个装饰器,即:

app.config(['$provide', function($provide) {
  $provide.decorator('numberFilter', ['$delegate', function($delegate) {
    var srcFilter = $delegate;

    var extendsFilter = function() {
      // Put the logic you want here:
      let val = arguments[0];
      if (angular.isNumber(val)) {
        return srcFilter.apply(this, arguments);
      }
      else if (angular.isString(val)) {
        return val;
      }
      else {
        // Or throw an error
        return JSON.stringify(val);
      }
    }

    return extendsFilter;
  }])
}]);

请参阅此plunker