Ember.js 如何从ember中的模板访问路由变量
我正在尝试使用余烬操作在余烬中进行表单验证。Ember.js 如何从ember中的模板访问路由变量,ember.js,ember-cli,Ember.js,Ember Cli,我正在尝试使用余烬操作在余烬中进行表单验证。 下面是我的add.js(路线): 这是我的模板代码: <h1>Add Contact</h1> <div class="full-width"> <div>{{input class="form-tag half-width" type="text" placeholder="Enter firstName" value=model.firstName}}</div> <
下面是我的add.js(路线): 这是我的模板代码:
<h1>Add Contact</h1>
<div class="full-width">
<div>{{input class="form-tag half-width" type="text" placeholder="Enter firstName" value=model.firstName}}</div>
<div>{{input class="form-tag half-width" type="text" placeholder="Enter lastName" value=model.lastName}}</div>
<div>{{input class="form-tag half-width" type="number" placeholder="Enter mobile Number" value=model.mobile}}</div>
<button class="form-button half-width" {{action 'saveContact' model}}> Add </button>
{{#if errorMessage}}
<div class="form-tag half-width">{{errorMessage}}</div>
{{/if}}
</div>
添加联系人
{{input class=“form tag half width”type=“text”placeholder=“Enter firstName”value=model.firstName}
{{input class=“form tag half width”type=“text”placeholder=“Enter lastName”value=model.lastName}
{{input class=“form tag half width”type=“number”placeholder=“Enter mobile number”value=model.mobile}
添加
{{{#如果错误消息}
{{errorMessage}}
{{/if}
在这个模板中,当有空白表单字段时,errorMessage字段不会显示。但是如果我在控制台中记录它,它就会显示。
有人能帮我吗
提前感谢
错误消息
,您的操作保存联系人
应该在相应的控制器上,而不是路由上。如果尚未创建相应的控制器,则可以使用ember generate controller
进行创建。控制器名称应与路由名称相对应
顺便说一句,当您从控制器操作发出该调用时,您需要使用
transitionToRoute
而不是transitionTo
谢谢!!但是如果我使用的是一个控制器,我可以以“model.firstName”的形式访问表单字段吗。。?或者我应该在Controlleries中定义单独的变量。除了将操作和其他属性移动到控制器之外,您可以保持路线不变。随时乐意提供帮助。:-)我可以在控制器中使用WillTransition方法吗?否。WillTransition在路由类上,而不是控制器上。
<h1>Add Contact</h1>
<div class="full-width">
<div>{{input class="form-tag half-width" type="text" placeholder="Enter firstName" value=model.firstName}}</div>
<div>{{input class="form-tag half-width" type="text" placeholder="Enter lastName" value=model.lastName}}</div>
<div>{{input class="form-tag half-width" type="number" placeholder="Enter mobile Number" value=model.mobile}}</div>
<button class="form-button half-width" {{action 'saveContact' model}}> Add </button>
{{#if errorMessage}}
<div class="form-tag half-width">{{errorMessage}}</div>
{{/if}}
</div>