angular 过滤器(日期转换,时间转换,数据转换等)

前端之家收集整理的这篇文章主要介绍了angular 过滤器(日期转换,时间转换,数据转换等)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="cnblogs_code">

('use strict'</span><span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt;*
 * myApp Module
 *
 * Description
 </span><span style="color: #008000"&gt;*/</span><span style="color: #000000"&gt;
angular.module(</span>'myApp'<span style="color: #000000"&gt;)
    .filter(</span>'is_null',<span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt;() {
        </span><span style="color: #0000ff"&gt;return</span> <span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt;(input) {
            </span><span style="color: #0000ff"&gt;return</span> input || '/'<span style="color: #000000"&gt;;
        }
    })
    .filter(</span>'star',<span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt;() {
        </span><span style="color: #0000ff"&gt;return</span> <span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt;(input) {
            </span><span style="color: #0000ff"&gt;return</span> Math.floor(input) || 0<span style="color: #000000"&gt;;
        }
    })
    .filter(</span>'unstar',<span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt;() {
        </span><span style="color: #0000ff"&gt;return</span> <span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt;(input) {
            </span><span style="color: #0000ff"&gt;return</span> 5 - Math.ceil(input) || 0<span style="color: #000000"&gt;;
        }
    })
    .filter(</span>'percent',<span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt;() {
        </span><span style="color: #0000ff"&gt;return</span> <span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt;(input) {
            </span><span style="color: #0000ff"&gt;return</span> (input * 100).toFixed(2) || 0<span style="color: #000000"&gt;;
        }
    })
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 毫秒转天时分</span>
    .filter('formatDuring',<span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt;() {
        </span><span style="color: #0000ff"&gt;return</span> <span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt;(mss) {
            </span><span style="color: #0000ff"&gt;var</span> days = parseInt(mss / (1000 * 60 * 60 * 24<span style="color: #000000"&gt;));
            </span><span style="color: #0000ff"&gt;var</span> hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60<span style="color: #000000"&gt;));
            </span><span style="color: #0000ff"&gt;var</span> minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60<span style="color: #000000"&gt;));
            </span><span style="color: #0000ff"&gt;var</span> seconds = (mss % (1000 * 60)) / 1000<span style="color: #000000"&gt;;
            </span><span style="color: #0000ff"&gt;return</span> days + " 天 " + hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒 "<span style="color: #000000"&gt;;
        }
    })
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;时间戳转换</span>
    .filter('getWeek',<span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt;() {
        </span><span style="color: #0000ff"&gt;return</span> <span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt;(input) {
            </span><span style="color: #0000ff"&gt;var</span> date = <span style="color: #0000ff"&gt;new</span> Date(input * 1000<span style="color: #000000"&gt;);
            </span><span style="color: #0000ff"&gt;var</span> week =<span style="color: #000000"&gt; date.getDay();
            </span><span style="color: #0000ff"&gt;switch</span><span style="color: #000000"&gt; (week) {
                </span><span style="color: #0000ff"&gt;case</span> 0<span style="color: #000000"&gt;:
                    </span><span style="color: #0000ff"&gt;return</span> "星期天"<span style="color: #000000"&gt;;
                    </span><span style="color: #0000ff"&gt;break</span><span style="color: #000000"&gt;;
                </span><span style="color: #0000ff"&gt;case</span> 1<span style="color: #000000"&gt;:
                    </span><span style="color: #0000ff"&gt;return</span> "星期一"<span style="color: #000000"&gt;;
                    </span><span style="color: #0000ff"&gt;break</span><span style="color: #000000"&gt;;
                </span><span style="color: #0000ff"&gt;case</span> 2<span style="color: #000000"&gt;:
                    </span><span style="color: #0000ff"&gt;return</span> "星期二"<span style="color: #000000"&gt;;
                    </span><span style="color: #0000ff"&gt;break</span><span style="color: #000000"&gt;;
                </span><span style="color: #0000ff"&gt;case</span> 3<span style="color: #000000"&gt;:
                    </span><span style="color: #0000ff"&gt;return</span> "星期三"<span style="color: #000000"&gt;;
                    </span><span style="color: #0000ff"&gt;break</span><span style="color: #000000"&gt;;
                </span><span style="color: #0000ff"&gt;case</span> 4<span style="color: #000000"&gt;:
                    </span><span style="color: #0000ff"&gt;return</span> "星期四"<span style="color: #000000"&gt;;
                    </span><span style="color: #0000ff"&gt;break</span><span style="color: #000000"&gt;;
                </span><span style="color: #0000ff"&gt;case</span> 5<span style="color: #000000"&gt;:
                    </span><span style="color: #0000ff"&gt;return</span> "星期五"<span style="color: #000000"&gt;;
                    </span><span style="color: #0000ff"&gt;break</span><span style="color: #000000"&gt;;
                </span><span style="color: #0000ff"&gt;case</span> 6<span style="color: #000000"&gt;:
                    </span><span style="color: #0000ff"&gt;return</span> "星期六"<span style="color: #000000"&gt;;
                    </span><span style="color: #0000ff"&gt;break</span><span style="color: #000000"&gt;;
            }
        }

    })
    .filter(</span>'snapshot',<span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt;() {
        </span><span style="color: #0000ff"&gt;return</span> <span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt;(input) {
            input </span>= input || ''<span style="color: #000000"&gt;;
            </span><span style="color: #0000ff"&gt;var</span> output = input.split('|')[0<span style="color: #000000"&gt;];
            </span><span style="color: #0000ff"&gt;return</span><span style="color: #000000"&gt; output;
        }
    })

})();

原文链接:https://www.f2er.com/angularjs/74088.html

猜你在找的Angularjs相关文章