【发布时间】:2016-12-31 09:24:15
【问题描述】:
我刚开始用 Mean.js 开发,在 angular 部分有点问题。
我在 node 中创建了 api,可以使用配方模型进行基本的 crud。
现在我正在尝试显示现有食谱的列表。
控制器:
'use strict';
angular.module('recipe').controller('RecipeController', ['$scope', 'RecipeService',
function ($scope, RecipeService) {
// Recipe controller logic
var vm = this;
vm.recipes = RecipeService.query();
}
]);
查看:
<section data-ng-controller="RecipeController">
<div data-ng-repeat="recipe in vm.recipes">
{{recipe.title}}
</div>
</section>
服务:
'use strict';
angular.module('recipe').factory('RecipeService', [ '$resource',
function ($resource) {
// Recipe service logic
var Recipe = $resource('api/recipes/:recipeId', {
recipeId: '@_id'
}, {
update: {
method: 'PUT'
}
});
angular.extend(Recipe.prototype, {
createOrUpdate: function () {
var recipe = this;
return createOrUpdate(recipe);
}
});
function createOrUpdate(recipe) {
if(recipe._id) {
return recipe.$update(onSuccess, onError);
} else {
return recipe.$save(onSuccess, onError);
}
function onSuccess(recipe) {
}
function onError(errorResponse) {
var error = errorResponse.data;
handleError(error);
}
}
function handleError(error) {
console.log(error);
}
// Public API
return Recipe;
}
]);
所以,当我在控制器中控制台记录 vm.recipe,然后查看异步调用是否会处理内容时,我会在 vm.recipes 中看到 2 个结果,这是正确的,因为我在 DB 中有 2 个结果。这种证明服务工作正常并加载数据。此外,它看起来在控制器中没问题,因为它会在那里检索数据。但是,这些数据不会加载到视图中,我不知道为什么。在配置中正确选择了视图。
有人知道吗?
【问题讨论】:
标签: javascript angularjs meanjs