是否可以在AngularJS中的已定义路径中传递您自己的变量?
我这样做的原因是因为我必须对同一页面的数据表示(一个是根据JSON数据的过滤视图),我需要做的就是给$params数组一个布尔标志让控制器功能知道该页面已过滤或未过滤.
像这样的东西:
var Ctrl = function($scope,$params) { if($params.filtered) { //make sure that the ID is there and use a different URL for the JSON data } else { //use the URL for JSON data that fetches all the data } }; Ctrl.$inject = ['$scope','$routeParams']; angular.modlule('App',[]).config(['$routeProvider',function($routes) { $routes.when('/full/page',{ templateURL : 'page.html',controller : Ctrl }); $routes.when('/full/page/with/:id',controller : Ctrl,params : { filtered : true } }); }]);
根据
原文链接:https://www.f2er.com/angularjs/142382.html$routeProvider
documentation,$routeProvider.when()的route参数具有属性resolve:
An optional map of dependencies which should be injected into the controller.
像这样的东西应该工作:
function Ctrl($scope,isFiltered) { if(isFiltered) { //make sure that the ID is there and use a different URL for the JSON data } else { //use the URL for JSON data that fetches all the data } } Ctrl.$inject = ['$scope','isFiltered']; angular.modlule('App',function($routeProvider) { $routeProvider.when('/full/page',{ templateURL: 'page.html',controller: Ctrl }); $routeProvider.when('/full/page/with/:id',controller: Ctrl,resolve: { isFiltered: function() { return true; } } }); }]);