Anu*_*bha 4 javascript url-routing angularjs angular-routing
我尝试了什么:
$routeProvider
.when('/paintings',
{
controller: 'imageController' , 'getPaintingImages'
templateUrl: 'paintings.html'
})
.when('/foods',
{
controller: 'imageController' , 'getFoodImages'
templateUrl: 'food.html'
})
Run Code Online (Sandbox Code Playgroud)
我想让getPaintingImages和getFoodImages从工厂获取绘画/食物列表,并使用imageController来操作图像.但只有第一个控制器被调用.
之前我写过代码只在imageController中获取图像,
myWebsite.controller('imageController', function imageController($scope, getPaintings){
$scope.images = getPaintings.images(); // but need to make this work for different set of images
$scope.imageCount = countObjectElements($scope.images);
$scope.selectedImage = $scope.images[0];
$scope.selectedImageIndex = 0;
$scope.updateSelectedImage = function(img) {
$scope.selectedImage = img;
$scope.selectedImageIndex = $scope.images.indexOf(img);
};
$scope.updateSelectedImageIndex = function(val) {
alert($scope.imageOf);
if($scope.selectedImageIndex <= 0)
$scope.selectedImageIndex = $scope.imageCount;
$scope.selectedImageIndex = ($scope.selectedImageIndex + val) % $scope.imageCount;
$scope.selectedImage = $scope.images[$scope.selectedImageIndex];
};
});
Run Code Online (Sandbox Code Playgroud)
由于我是angularJS的初学者,我不确定是否为多个控制器创建了重用imageController的解决方案?如果是,如何执行此操作,如果不是如何重新使用imageController来处理不同的图像集.在函数的情况下,函数的重用通常是通过参数传递.但是在这里我想知道控制器如何在内部调用视图时获取参数?
小智 8
getPaintingImages和getFoodImages正在使用工厂来获取您说的图像.听起来你可以在routeProvider中使用像resolve这样的东西,这样在调用时,imageController需要的图像就在那里.
类似的东西(假设你的getPaintings和getFoods是服务/工厂和获取图像是返回$ promise的解析成图像,即$ http请求):
$routeProvider
.when('/paintings', {
controller: 'imageController',
templateUrl: 'paintings.html',
resolve: {
images: function($q, getPainting) {
getPainting.images();
}
}
})
.when('/foods', {
controller: 'imageController',
templateUrl: 'food.html',
resolve: {
images: function($q, getFoods) {
getFoods.images();
}
}
})
Run Code Online (Sandbox Code Playgroud)
然后你可以访问像这样的图像:
myWebsite.controller('imageController', ['$scope', 'images', function ($scope, images){
...
}]);
Run Code Online (Sandbox Code Playgroud)
如何使imageController成为父:
<body ng-controller="imageController">
<div ng-view></div>
</body>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23922 次 |
| 最近记录: |