加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS为路由内定义的控制器预先提供$params变量

发布时间:2020-12-17 07:33:41 所属栏目:安全 来源:网络整理
导读:是否可以在AngularJS中的已定义路径中传递您自己的变量? 我这样做的原因是因为我必须对同一页面的数据表示(一个是根据JSON数据的过滤视图),我需要做的就是给$params数组一个布尔标志让控制器功能知道该页面已过滤或未过滤. 像这样的东西: var Ctrl = funct
是否可以在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
    }
  });

}]);
根据 $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; }
    }
  });

}]);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读