绑定在视图和控制器之间不起作用
Binding does not work between view and controller
我在 Mean.js 才开始开发,angular 部分有点问题。
我在节点中创建了 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.recipe 中看到 2 个结果,这是正确的,因为我有2 结果在数据库中。这种证明服务工作正常并加载数据。此外,它看起来在控制器中没问题,因为它将在那里检索数据。但是,这些数据不会加载到视图中,我不知道为什么。在配置中正确选择了视图。
有人知道吗?
好的,没找到为什么它不能以这种形式工作,但我可以通过简单地将食谱放入 $scope 属性.
来让它工作
'use strict';
angular.module('recipe').controller('RecipeController', ['$scope', 'RecipeService',
function ($scope, RecipeService) {
// Recipe controller logic
$scope.recipes = RecipeService.query();
}
]);
请试试这个
在
的地方
var vm = this;
vm.recipes = RecipeService.query();
尝试使用
$scope.recipes = RecipeService.query();
在控制器中
并在 html
在的地方
采用
只有
我在 Mean.js 才开始开发,angular 部分有点问题。
我在节点中创建了 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.recipe 中看到 2 个结果,这是正确的,因为我有2 结果在数据库中。这种证明服务工作正常并加载数据。此外,它看起来在控制器中没问题,因为它将在那里检索数据。但是,这些数据不会加载到视图中,我不知道为什么。在配置中正确选择了视图。
有人知道吗?
好的,没找到为什么它不能以这种形式工作,但我可以通过简单地将食谱放入 $scope 属性.
来让它工作'use strict';
angular.module('recipe').controller('RecipeController', ['$scope', 'RecipeService',
function ($scope, RecipeService) {
// Recipe controller logic
$scope.recipes = RecipeService.query();
}
]);
请试试这个
在
的地方var vm = this;
vm.recipes = RecipeService.query();
尝试使用
$scope.recipes = RecipeService.query();
在控制器中
并在 html 在的地方 采用 只有