我剛開始在Mean.js中開發,並且在角度部分有點問題。綁定在視圖和控制器之間不起作用
我在節點裏創建了api,可以用配方模型做基本的泥濘。
現在我試圖顯示現有食譜的列表。
控制器:
'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個結果,這是正確的,因爲我在數據庫中有2個結果。這種證明服務工作正常並加載數據。此外,它看起來像在控制器中,因爲它會在那裏檢索數據。但是,這些數據不會被加載到視圖中,我不知道爲什麼。該視圖在配置中正確選擇。
有沒有人有任何想法?
我們都是對的。實際上,我發現這個虛擬機示例沒有起作用,它在mean.js示例模塊中做了。有一些標誌將範圍映射爲vm屬性。 –
@MichalTakáč您是否指'client.routes.js'文件中的'controllerAs:'vm''? –
@LukeKroon不,我不是,那是錯誤的。 –