【问题标题】:Meteor two way data binding for form validationMeteor 用于表单验证的双向数据绑定
【发布时间】:2014-04-21 22:40:31
【问题描述】:

我阅读了很多关于 Blaze 允许 Meteor 0.8 反应式渲染的资源,但我似乎无法为下面的简单问题找到解决方案。

我正在尝试验证我的表单输入。为了简单起见,假设我只想在提交表单时更改我的{{message}}。我在client.js 中采用的方法只是给辅助变量一个新值。这是我过去使用 AngularJS 的方式,但似乎不仅仅是更改 Meteor 中的变量。我该怎么办?

- index.html
<template name="user">
    <form>
        <input type="text" id="name">
        <p>{{message}}</p>
        <button class="submit" onclick="return false;">Submit</button>
    </form>
</template>

- client.js
Template.user.message = "";
Template.user.events = {
    'click .submit' = function(){
        Template.user.message = "valid";
    }
}

【问题讨论】:

    标签: javascript data-binding meteor


    【解决方案1】:

    如果您使用反应变量,它应该可以工作。我将在本例中使用会话变量:

    Template.user.message = function() {
      return Session.get('userMessage');
    };
    
    Template.user.events({
      submit: function() {
        Session.set('userMessage', 'valid');
      }
    });
    

    请注意,events 接受一个对象(您的代码正在分配点击处理程序,而不是在事件映射中创建值)。

    【讨论】:

    • 我觉得一定有一些AngularJS的方法,因为处理会话变量要麻烦得多。但这绝对是一个解决方案。
    猜你喜欢
    • 2017-05-10
    • 1970-01-01
    • 2011-01-19
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多