javascript - 请问 avalon 1.5.8 组件 $template 里能否使用三元表达式?
怪我咯
怪我咯 2017-04-11 10:57:06
[JavaScript讨论组]
  • 使用 avalon.mobile.js 1.5.8

  • 想用组件动态添加数据,需要用判断参数el.oper_status, 如ms-visible={{el.oper_status === 'ADD'}},但是一直报错Uncaught SyntaxError: Unexpected identifier

  • ms-class 里使用三元表达式也不成功 ms-class={{el.oper_status === 'ADD' ? 'add-num' : ''}}

  • 插值表达式也不可以用三元表达式{{el.oper_status === 'ADD' ? '+' : '-'}}

  • 具体代码如下,是我想实现的效果,跑不起来。。请问这种情况应该如何写呢?

  • html

  • js

    
    function addPageVm(i) {
    $.component("ms:p", {
        list: i.list,
        $ready: function() {
            console.log("构建完成")
        },
        time: function (index) {
            var time = new Date(i.list[index].created_at/1000),
                Y = time.getFullYear() + '-',
                M = (time.getMonth()+1 < 10 ? '0'+(time.getMonth()+1) : time.getMonth()+1) + '-',
                D = time.getDate() + ' ',
                h = time.getHours() + ':',
                m = time.getMinutes() + ':',
                s = time.getSeconds();
            return Y+M+D+h+m+s
        },
        $template: '

    ' + '

    {{el.description}}

    ' + '

    ' + '

    ' + '{{el.oper_status === 'ADD' ? '+' : '-'}}' + '{{el.oper_point}}

    ' }); $.scan(); }
怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(1)
阿神

在 Github 上骚扰了大神,但是发现不是 avalon 的问题,(°ー°〃) 应该是语法问题,$template 里没法直接用字符串和 : 符号,把要用的字符串先定义一下就能用了哦!

function addPageVm(i) {
                $.component("ms:p", {
                    $replace: 1,
                    $ready: function() {
                        console.log("构建完成")
                    },
                    add: "ADD",
                    addSign: '+',
                    minusSign: '-',
                    time: function (index) {
                        var time = new Date(i.data.list[index].created_at/1000),
                            Y = time.getFullYear() + '-',
                            M = (time.getMonth()+1 < 10 ? '0'+(time.getMonth()+1) : time.getMonth()+1) + '-',
                            D = time.getDate() + ' ',
                            h = time.getHours() + ':',
                            m = time.getMinutes() + ':',
                            s = time.getSeconds();
                        return Y+M+D+h+m+s
                    },
                    $template: '<p class="single" ms-repeat="list" ms-visible="el.oper_status === add">' +
                '<p class="head">{{el.description}}</p>' +
                '<p class="detail" ms-text="time($index)"></p>' +
                '<p ms-if="el.oper_status === add" class="sub-num add-num" >' +
                '<span class="symbol">{{el.oper_status === add ? addSign : minusSign}} </span>' +
                '<span>{{el.oper_point}}</span></p>' +
                '<p ms-if="el.oper_status !== add" class="sub-num" >' +
                '<span class="symbol">{{el.oper_status !== add ? addSign : minusSign}} </span>' +
                '<span>{{el.oper_point}}</span></p>' +
                '<ms:text /></p>'
                });
                $.scan();
            }
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号