V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
cstome
V2EX  ›  问与答

angularJS 可以在{{}}内放 function 吗?

  •  
  •   cstome · Sep 17, 2016 · 2254 views
    This topic created in 3521 days ago, the information mentioned may be changed or developed.

    最近刚接触 AngularJS ,用到 ng-repeat ,但是 json 中有个数据是 timestamp ,需要解析成可以看的时间,这个要怎么处理啊?

    6 replies    2016-09-17 23:18:15 +08:00
    Exin
        1
    Exin  
       Sep 17, 2016 via Android   ❤️ 1
    了解下 angular 的 filter
    stiekel
        2
    stiekel  
       Sep 17, 2016   ❤️ 1
    使用 Angular 本身提供的 date filter 来解决 https://docs.angularjs.org/api/ng/filter/date
    cstome
        3
    cstome  
    OP
       Sep 17, 2016
    @stiekel 还有个问题,加入我想用自己的 function 来解析 timestamp ,

    <div class="time">{{x.t | praseTime}}</div>
    app.filter('praseTime', function (x.t){return praseTS(x.t)});

    这样写为什么不行?
    filter 要写在 controller 前面还是后面还是里面?
    bdbai
        4
    bdbai  
       Sep 17, 2016 via Android
    @cstome 没用过 Angular 的人建议你把 function 里的 x.t 全改成 t 试试。
    stiekel
        5
    stiekel  
       Sep 17, 2016
    @cstome 如果你只是要显示时间,最好用 Angular 原生的 date filter 。

    filter 写在控制器外面,前面或者后面均可,因为它会先于控制器加载。

    你的过滤器写错了。而且,参数传递,不要使用 x.t 这种。
    cstome
        6
    cstome  
    OP
       Sep 17, 2016
    @stiekel
    我发现 ng-repeat=" x in xxx"的时候, x 是 object , t 是 x 的一个属性,在写 filter 的时候传递 x 就是{{x.t}}里面的值,也就是说不是传递 x.t ,而是 x 。。

    我之前想错了。。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2963 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 81ms · UTC 14:58 · PVG 22:58 · LAX 07:58 · JFK 10:58
    ♥ Do have faith in what you're doing.