相关疑难解决方法(0)

如何在angular中将依赖注入到module.config(configFn)中

-在角度,我们可以注入$routeProviderconfig功能

module.config(function ($routeProvider) {


});
Run Code Online (Sandbox Code Playgroud)

我想把我的服务注入其中

module.config(function ($routeProvider, myService) {


});
Run Code Online (Sandbox Code Playgroud)

我确信该服务已正确定义,但它会抛出一个异常unknown myService,说明当我注入时的事件

module.config(function ($routeProvider, $http) {


});
Run Code Online (Sandbox Code Playgroud)

它仍然说unknown $http.

你知道为什么吗?

angularjs

74
推荐指数
4
解决办法
9万
查看次数

AngularJS路由中的动态部分参数

我正在使用angularjs网站,并且在Rails中使用路由以及在php中使用Laravel.通过Laravel中的路线,我们可以动态创建一组类似于以下的路线:

  foreach($cities as $city):
    Route::get($city.'/hotels');
    Route::get($city.'/{slug}');

  endforeach;                      
Run Code Online (Sandbox Code Playgroud)

在这里,我们定义一系列Laravel单独的路线在技术上做看起来是一样的,除了价值的cityslug.

在这种情况下,我发现angularJS在定义路径时有点受限.坦率地说,我有点迷失在这里.

UPDATE

我在这里做了一些修改 - 基本上我设置了一个服务,它从我的数据库中检索资产,例如在这种情况下的城市和类别列表.我正在尝试这样做:

如果{slug}在我的API中检索的类别数组中,则使用my ListController和list视图,但如果不是,则使用我的SingleVenueController单视图.这是我目前的代码,但它无法正常工作:(

  appRouteProvider.when('/:city/:slug', {
      templateUrl : function(sharedParams, $routeParams){
        t = sharedParams.getCurrentPageType($routeParams);
        if(t=='list'){
          return '../../app/templates/list.html';
        }
        if(t=='single'){
          return '../../app/templates/single.html';
        }

      },
      controller  :  function(sharedParams, $routeParams){
        t = sharedParams.getCurrentPageType($routeParams);
        if(t=='list'){
          return 'ListsController';
        }
        if(t=='single'){
          return 'SingleController';
        }
      },


    resolve:{
      sharedParamsData:function(sharedParams){
        return sharedParams.promise;
      },
    }
  })
Run Code Online (Sandbox Code Playgroud)

在上面sharedParams是一个服务,getCurrentPageType只是检查url slug来决定要发回的控制器 - 但它根本不工作:(

javascript php laravel angularjs

14
推荐指数
1
解决办法
475
查看次数

是否可以使用拦截器将GET/POST参数添加到Angular $ http请求中

假设我需要在用户发出的每个请求中包含GroupId参数,但我不想修改每个服务调用以包含它.是否可以将GroupId自动附加到所有请求,无论是POST还是GET查询字符串?

我一直在研究拦截器request功能,但无法弄清楚如何进行更改

**编辑**

下面的当前工作样本是Morgan Delaney和haimlit建议的组合(我认为无论如何它都是一个组合).基本思想是,如果请求是POST,请修改config.data.对于GET,请修改params.到目前为止似乎工作.

还不清楚提供者系统如何在Angular中工作,所以我不确定在这里修改data.params属性是否完全合适.

.config(['$httpProvider', function ($httpProvider) {
  $httpProvider.interceptors.push(['$rootScope', '$q', 'httpBuffer', function ($rootScope, $q, httpBuffer) {
      return {

          request: function (config) {

              if (config.data === undefined) {
                  //Do nothing if data is not originally supplied from the calling method
              }
              else {
                  config.data.GroupId = 7;
              }

              if (config.method === 'GET') {
                  if (config.params === undefined) {
                      config.params = {};
                  }
                  config.params.GroupId = 7;
                  console.log(config.params);
              }

              return config;
          }
      };
  } ]);
 } …
Run Code Online (Sandbox Code Playgroud)

angularjs angular-http-interceptors

9
推荐指数
1
解决办法
6208
查看次数

Angular中的未知$ http提供程序

我需要$http在config方法中使用service.但我无法$http在配置中使用,我收到unknown Provider错误消息.

我的代码:

.config(function($http, $routeProvider, $provide) {
    $http.get("sampleslist.js").success(function(data) {
        var loop = 0, currentRoute;

        for (loop = 0; loop < data[loop].pages.length; loop++) {
            currentRoute = data[loop].pages;

            var routeName = "/" + currentRoute[loop].name;
            $routeProvider.when(routeName, {
                templateUrl:"/" + currentRoute.name + ".html",
            })
        }
    })

    app = {controller: $controllerProvider.register}
})
Run Code Online (Sandbox Code Playgroud)

你能为此提供解决方案吗?

javascript angularjs angular-routing angularjs-directive

9
推荐指数
1
解决办法
312
查看次数