标签: angularjs-service

AngularJS结合了Web服务响应

我有两个网络服务:

一个人返回"文章",如下所示:

[   
    {
        "id": "1",
        "headline": "some text",
        "body": "some text",
        "authorId": "2"
    },
    {
        "id": "2",
        "headline": "some text",
        "body": "some text",
        "authorId": "1"
    }
]
Run Code Online (Sandbox Code Playgroud)

另一个返回像这样的"作者",给出一个id:

{
    "id": "1",
    "name": "Test Name",
    "email": "test@test.com",
    "photo": "path/to/img"
}
Run Code Online (Sandbox Code Playgroud)

我想将两者结合起来,这样我就可以在文章概览列表中显示作者姓名和照片.

像这样:

[   
    {
        "id": "1",
        "headline": "some text",
        "body": "some text",
        "authorId": "2",
        "author_info": {
            "id": "2",
            "name": "Another Test Name",
            "email": "test2@test.com",
            "photo": "path/to/img"
        }
    },
    {
        "id": "2",
        "headline": "some text",
        "body": "some text",
        "authorId": "1"
        "author_info": …
Run Code Online (Sandbox Code Playgroud)

ajax angularjs angularjs-service

5
推荐指数
2
解决办法
6189
查看次数

AngularJS - 在页面加载时将数据引导到服务中

在angularjs应用程序中,我试图使用自定义服务来保存应用程序的主要数据"项目".但是,我想在初始页面加载时将数据引导到服务中,以避免向服务器发出单独的ajax请求.这样做最干净的方法是什么?

以下是我的服务片段供参考:

app.factory('items', function() {
    var itemsService = {},
        items = [];

    itemsService.list = function() {
        return items;
    };

    itemsService.add = function() {
        /* ... */
    };

    return itemsService;
});
Run Code Online (Sandbox Code Playgroud)

在后端我使用node.js + expressjs + jade,所以我将使用以下方法将数据注入页面:

!{JSON.stringify(items)}
Run Code Online (Sandbox Code Playgroud)

bootstrapping angularjs angularjs-service

5
推荐指数
1
解决办法
4227
查看次数

Angular:在模块的配置/运行中混合提供者和自定义服务

我想做那样的somtehing:

angular.module('app', []).config(
  [ '$httpProvider', 'customAuthService',
    ($httpProvider, customAuthService) ->
      $httpProvider.defaults.transformRequest.push (data) ->
        if customAuthService.isLoggedIn
          data['api_key'] = {token: @token}
  ])
Run Code Online (Sandbox Code Playgroud)

根据Angularjs doc,我不能在config我的块中执行它module,因为那里不允许自定义服务,也不能在run块中执行,因为$httpProvider不允许那样的提供者:

配置块 - 在提供程序注册和配置阶段执行.只有提供程序和常量才能注入配置块.这是为了防止在完全配置服务之前意外实例化服务.

运行块 - 在创建注入器后执行并用于启动应用程序.只有实例和常量才能注入运行块.这是为了防止在应用程序运行时进一步进行系统配置.

如何在$httpProvider依赖自制服务的情况下添加一些配置呢?

javascript angularjs angularjs-service

5
推荐指数
1
解决办法
4242
查看次数

简单的Angular $ routeProvider解析测试.这段代码有什么问题?

我创建了一个简单的Angular JS $ routeProvider解析测试应用程序.它给出以下错误:

Error: Unknown provider: dataProvider <- data
Run Code Online (Sandbox Code Playgroud)

如果有人能够确定我哪里出错了,我将不胜感激.

的index.html

<!DOCTYPE html>
<html ng-app="ResolveTest">
  <head>
    <title>Resolve Test</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.js">    </script>
    <script src="ResolveTest.js"></script>
  </head>
  <body ng-controller="ResolveCtrl">
    <div ng-view></div>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

ResolveTest.js

var rt = angular.module("ResolveTest",[]);

rt.config(["$routeProvider",function($routeProvider)
{
  $routeProvider.when("/",{
    templateUrl: "rt.html",
    controller: "ResolveCtrl",
    resolve: {
      data: ["$q","$timeout",function($q,$timeout)
      {
        var deferred = $q.defer();

        $timeout(function()
        {
          deferred.resolve("my data value");
        },2000);

        return deferred.promise;
      }]
    }
  });
}]);

rt.controller("ResolveCtrl",["$scope","data",function($scope,data)
{
  console.log("data : " + data);
  $scope.data = data;
}]);
Run Code Online (Sandbox Code Playgroud)

rt.html

<span>{{data}}</span>
Run Code Online (Sandbox Code Playgroud)

angularjs angularjs-directive angularjs-service angularjs-routing angularjs-controller

5
推荐指数
1
解决办法
1万
查看次数

在服务中使用$ routeParams

我正在尝试将查询参数附加到我从URL获取的所有API调用.

factory('Post', function ($resource, $routeParams) {
    return $resource('api/posts', {customer_id: $routeParams.customerId});
})
Run Code Online (Sandbox Code Playgroud)

这在第一次使用Post服务时工作,但第二次注入时它已经初始化并且正在使用第一个客户ID,即使URL已经更改.

我该如何按预期工作?

angularjs angularjs-service

5
推荐指数
1
解决办法
4544
查看次数

这是一个伪装成工厂的AngularJS服务吗?

我创建了一个AngularJS工厂,我重用它来进行数据访问:

app.factory('abstractFactory3', function ($http) {

    function abstractFactory3(odataUrlBase) {
        this.odataUrlBase = odataUrlBase;
    }

    abstractFactory3.prototype = {
        getList: function (odataOptions) {
            return $http.get(this.odataUrlBase, {
                params: odataOptions
            });
        },
        get: function (id, odataOptions) {
            return $http.get(this.odataUrlBase + '/' + id, {
                params: odataOptions
            });
        },
        insert: function (data) {
            return $http.post(this.odataUrlBase, data);
        },
        update: function (id, data) {
            return $http.put(this.odataUrlBase + '(' + id + ')', data);
        },
        remove: function (id) {
            return $http.delete(this.odataUrlBase + '(' + id + ')');
        }
    };

    return …
Run Code Online (Sandbox Code Playgroud)

javascript jquery http angularjs angularjs-service

5
推荐指数
1
解决办法
152
查看次数

获取 $state.current (ui-router) 进入 $http 拦截器

我正在努力实现的目标

我想在我的 $http 拦截器中获取当前状态的数据值。我已经创建了拦截器。

问题

我不知道如何在拦截器中捕获 $state.current.data。我尝试使用 $injector.get($state) 但无法读取对象的属性。

 angular.module('mymodule').factory 'HttpInterceptor', [
 '$location', '$injector'
  ($location, $injector) ->

  request: (config) ->
   # Do something when sending the request
   console.log(config.headers.hasOwnProperty('Authorization'))
   if config.headers.hasOwnProperty('Authorization') == false
    $location.url("/login")
   config
Run Code Online (Sandbox Code Playgroud)

angularjs angularjs-service angular-ui-router

5
推荐指数
1
解决办法
2882
查看次数

AngularJs中的类操作和实例操作有什么区别?

来自文档:

类操作返回空实例(下面有其他属性).实例操作返回操作的承诺

但是,文档没有明确区分类操作和实例操作.如果可能的话,你能指出一个很好的例子吗?

angularjs angularjs-service angularjs-resource

5
推荐指数
1
解决办法
729
查看次数

更新$ http回调中的服务变量

我正在使用一项服务将用户数据提供给我的Angular应用程序中的各种控制器.我不知道如何使用$ http服务来更新服务的本地变量(在我的例子中是"this.users").我曾经有过无望的尝试.服务器正确响应.

我已经阅读了几篇关于如何在服务中使用$ http来更新控制器范围的优秀文章.最好的是这个:http://sravi-kiran.blogspot.com/2013/03/MovingAjaxCallsToACustomServiceInAngularJS.html.这对我没有帮助,因为它否定了使用服务的好处.主要是,修改一个控制器中的范围不会在应用程序的其余部分中进行修改.

这是我到目前为止所拥有的.

app.service('UserService', ['$http', function($http) {
    this.users = [];

    this.load = function() {
        var promise = $http.get('users.json')
            .success(function(data){
                // this.users is undefined here
                console.log(this.users);
            }
    };

    promise.then(function() {
        // this.users is undefined here
        console.log('this.users');
    });
}]);
Run Code Online (Sandbox Code Playgroud)

任何帮助是极大的赞赏.谢谢.

angularjs angularjs-service

5
推荐指数
1
解决办法
3802
查看次数

如何使用不与使用它的控制器共享其值的服务对象

我们假设我创建了一个工厂,服务或提供者对象

myApp.factory('myService', function() {
  return {
    counter: 0,
    increment: function() {
      console.log('counter: ' + (this.counter++))
    }
  };
});
Run Code Online (Sandbox Code Playgroud)

假设我有一个依赖它的控制器

myApp.controller('myCtrl', function(myService) {
  $scope.incr = function() {
    myService.increment();
  };
}
Run Code Online (Sandbox Code Playgroud)

我将此控制器应用于html的不同部分

<div ng-controller="myCtrl">
  <button ng-click="increment()">increment</button>
</div>
...
<div ng-controller="myCtrl">
  <button ng-click="increment()">increment</button>
</div>
Run Code Online (Sandbox Code Playgroud)

现在,当我点击每个按钮时,计数器是通用的,它会变为0,1,2,3 ......

如何编写工厂,服务或提供程序,以便每个控制器获取服务对象的不同副本?

angularjs angularjs-service angularjs-factory angularjs-provider

5
推荐指数
1
解决办法
667
查看次数