我正在开发一个大型移动应用程序,但我遇到了一个争论。目前,我有各种其他api的资源连接到一个指令,然后在那时将数据加载到范围内。
使用angularjs从指令中的资源注入数据或在RouteProvider配置上使用resolve属性更好吗?
修改
指令:
angular.module('categories')
.directive( 'Category', [function() {
return {
restrict: 'EA',
priority: 1000,
controller: ['$scope', '$attrs', '$log', 'CategoryFactory', function($scope, $attrs, $log, CategoryFactory) {
var categoryObject = {};
var item = $scope.$eval($attrs.ppdCategory);
if( item !== "" && ! isNaN(parseInt(item, 10)) ) {
categoryObject.categoryId = item;
}
var $promise = CategoryFactory.get(categoryObject).$promise;
$promise
.then( function( result ) {
$scope.childCategories = result.response.childCategories;
$scope.category = result.response.category;
})
.catch( function( error ) {
$log.error( "An error ocurred while fetching categories" + error );
});
}]
};
}]);
控制器:
.config([ '$routeProvider', function($routeProvider) {
$routeProvider
.when( '/browse-aisles', {
reloadOnSearch: false,
controller: 'CategoryController',
// this will eventually be a function that
// manages the switching of the mobile, tablet,
// and full site templates
templateUrl: 'partials/pages/browse/browse_aisles.html'
})
.when( '/browse-aisles/:categories*', {
reloadOnSearch: false,
controller: 'CategoryController',
// this will eventually be a function that
// manages the switching of the mobile, tablet,
// and full site templates
templateUrl: 'partials/pages/browse/browse_aisles.html'
});
}])
.controller( 'CategoryController', [ '$scope', '$routeParams', 'keyedRouteService', 'SharedProperties', 'BROWSE_PARAMS', function( $scope, $routeParams, keyedRouteService, SharedProperties, BROWSE_PARAMS ) {
var categoryParams = typeof $routeParams.categories === 'undefined' ? "" : $routeParams.categories;
var params = keyedRouteService.getKeyedArray( "categories//" + categoryParams );
$scope.params = SharedProperties.setProperty(BROWSE_PARAMS, params );
$scope.$emit('loadSubHeader', 'partials/pages/browse/browse_aisles_subheader.html');
var baseUrl = "/browse-aisles/";
for( var category in $scope.params.categories ) {
if( $scope.params.categories[category] !== "" ) {
baseUrl += "categories/" + $scope.params.categories[category] + "/";
}
}
$scope.baseCategoryUrl = baseUrl;
}]);
解析将解析格式的网址: /浏览-过道/类别/ 123 /类别/ 1234 /类别/ 2444 /产品/ 233