javascript - Ember JS 模板不渲染模型值

标签 javascript json rest ember.js

这是我的 app/models/post.js (这是模型)

import DS from 'ember-data';

var attr = DS.attr;

export default DS.Model.extend({
  author: DS.belongsTo('author'),
  title: attr('string'),
  body: attr('string')
});

这是位于 app/routes/post.js 的路由

import Ember from 'ember';

export default Ember.Route.extend({
});

这是处理它的模板 app/templates/post.hbs

<h1>{{title}}</h1>
<h2>by {{author.name}}</h2>

<hr>
<div class = "body">
{{body}}
</div>

这是我的路由器。

Router.map(function() {
  this.route('about');
  this.resource('posts');
  this.resource('post',{ path: '/posts/:post_id'});
});

每当我访问 localhost:4200/post/1 时,该 View 不会呈现任何内容,但是每当我查看网络时,它都会检索此 json 数据

{"post":{"id":1,"title":"How to win at life","body":"Body","author":1},"authors":[{"id":1,"name":"George","posts":[1,2]}]}

这是我的目录结构

enter image description here

每当我运行调试器并将鼠标悬停在页面上时,它都会显示我有一个模型 enter image description here

最佳答案

您需要在模板中使用model属性:

<h1>{{model.title}}</h1>
<h2>by {{model.author.name}}</h2>

<hr>
<div class = "body">
{{model.body}}
</div>

ObjectController 现已弃用,因此 model 属性不再被代理,您不能仅使用 {{title}} 代替{{model.title}}

this.resource() 也是 deprecated now .

关于javascript - Ember JS 模板不渲染模型值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32161733/

相关文章:

javascript - 框架如何实现自定义HTML属性和双重 curl 等功能?

java - android studio 项目中的菜单栏

c# - 从 asp.net 网页中的 jQuery Ajax 调用检索 JSON 数组

rest - Web API url 的测试版本

java - 如何在java REST中使用引号

javascript - 在 javascript 中将 6*6 数组转换为 3*3 数组的问题

javascript - BiQuadFilters 与 MyOwnFilter - Javascript WebAudio API

javascript - Bootstrap 模式数据远程在 Chrome 中不起作用

json - 不跟踪更改的可变 JSON 对象类型 SQLAlchemy (sqlalchemy-json)

hibernate - 生命周期异常 : Failed to start component deploying war in Tomcat