标签: angularjs-service

AngularJS:PUT使用URL发送数据,但不作为JSON数据发送数据

这是我的 UserService

angular.module('userServices', ['ngResource']).factory('User', function($resource) {
  return $resource('/users/:userId',
      // todo: default user for now, change it
      {userId: 'bd675d42-aa9b-11e2-9d27-b88d1205c810'},
      {update: {method: 'PUT', params:{profile: '@profile'}, isArray: false}}
  );
});
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我做到了

$scope.save = function() {
    $scope.user.$update({profile: $scope.profile});
}
Run Code Online (Sandbox Code Playgroud)

但是当我在Chrome中看到网络标签时,我明白了

Request URL:http://localhost:5000/users/bd675d42-aa9b-11e2-9d27-b88d1205c810?profile=%5Bobject+Object%5D
Request Method:PUT
Status Code:200 OK
Run Code Online (Sandbox Code Playgroud)

如何将其作为data有效载荷发送?这URL就是

http://localhost:5000/users/bd675d42-aa9b-11e2-9d27-b88d1205c810
Run Code Online (Sandbox Code Playgroud)

数据如下

{
  day_in_month: 5
}
Run Code Online (Sandbox Code Playgroud)

我的端点期望数据是请求的一部分,因此它可以将其解析为 request.json

谢谢

javascript flask angularjs angularjs-service angularjs-scope

1
推荐指数
2
解决办法
7812
查看次数

如何使用字符串变量调用"Angular JS"服务方法

无论如何,使用字符串变量调用角度服务

$scope.serviceList=["yearDetails","monthDetails","dayDetails"];

//controller
$scope.getDetails=function(type,index){

    if(type==$scope.serviceList[index]){

    // if i will call like this yearDetails.query(function(data){}); it is working
    //here i am getting "yearDetails"
     $scope.serviceList[index].query(function(data){
            console.log(data);
     });
    }
}

//service
.factory('yearDetails', function($resource){
           return $resource('/getyearDetails', {}, {
               query: { method:'POST', params:{}, isArray:false }
   });
})
.factory('monthDetails', function($resource){
           return $resource('/getmonthDetails', {}, {
               query: { method:'POST', params:{}, isArray:false }
   });
})
.factory('dayDetails', function($resource){
           return $resource('/getdayDetails', {}, {
               query: { method:'POST', params:{}, isArray:false }
   });
})
Run Code Online (Sandbox Code Playgroud)

angularjs angularjs-service

1
推荐指数
2
解决办法
6038
查看次数

是否可以在没有模块的情况下创建AngularJS服务?

在AngularJS中有一个简短的表单来创建控制器,因此您不必为此使用模块.这个简短的形式是简单地定义一个函数

function FooController ($scope) {
  // ...
}
Run Code Online (Sandbox Code Playgroud)

并使用ng-controller以下方法从HTML标记中引用它:

<div ng-controller="FooController">
  ...
</div>
Run Code Online (Sandbox Code Playgroud)

现在,我的问题是,是否有类似的"简短形式"来定义服务?换句话说:您可以在不使用模块的情况下定义(和使用)服务吗?

请注意,我知道使用模块构建应用程序是完全有意义的,不这样做可能/应该被认为是不好的做法.只是我正在向一个对它全新的人解释AngularJS,而今天出现了这个问题.所以这只是为了好奇和完整.

angularjs angularjs-service

1
推荐指数
2
解决办法
1316
查看次数

Angular app中的"未知提供程序错误"

我按照Angular文档创建了我的服务并看到了一条错误消息

http://docs.angularjs.org/error/$injector:unpr?p0=$scopeProvider%20%3C-%20$scope%20%3C-%20$users

这是我的代码:

var angularApp = angular.module("myApp", []);
angularApp.run(function($http) {
  $http.defaults.headers.common['Content-Type'] = 'application/x-www-form-urlencoded';
  $http.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
});

angularApp.service('$users', function($scope, $http, $cookie, $window) {
  // need to instantiate the service
  var newUsersServiceInstance;

  // upper case is resource
  var User = $resource('/user/:userId', {userId:'@id'});
  var MyPassword = $resource('/mypasswords/new');
  // lower case is instance you tend to want the instance
  // to be binded to the controller's scope
  $scope.user // = User.get({userId:123});

  $scope.getUser = function(id, s) {
    $scope.user = User.get({"userId": id}, s, e);
  } …
Run Code Online (Sandbox Code Playgroud)

angularjs angularjs-service

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

如何创建一个具有可在AngularJS中的控制器之间共享的属性的对象?

这是如何创建此全局常量以在Angularjs中的控制器之间共享的后续问题

提供的答案允许在控制器之间共享一个恒定的$ webroot.

app = angular.module('myApp', []);
app.constant('$webroot', 'localhost/webroot/app');

app.controller('myController', ['$scope', '$webroot', function($scope, $webroot) {
  $scope.webroot = $webroot;
}]);
Run Code Online (Sandbox Code Playgroud)

然而,问题是如果我有10个常数,那么所有10个常数都必须注入控制器.这使控制器声明看起来长而丑陋.如何创建一个具有可在AngularJS中的控制器之间共享的属性的对象?这样,我只需要注入一个对象而不是多个常量.这可以在Angularjs中完成吗?谢谢.

javascript angularjs angularjs-service

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

返回undefined的角度服务不是一个函数

我在Angular控制器中有一个函数,如下所示:

app.controller("UsersController", ["$scope",  "UserList", function($scope, UserList) {

    $scope.createUserSubmit = function(newUser) {

      UserList.createUser().success(function(data){
        console.log(data);
      });

    }

}]);
Run Code Online (Sandbox Code Playgroud)

createUserSubmit()调用该函数时,它会将另一个函数调用到createUser()服务中的函数UserList.

该服务看起来像这样:

app.service("UserList", function() {
  this.createUser = function(){
    console.log("doing it");
    return "hi";
  }
});
Run Code Online (Sandbox Code Playgroud)

现在,当这一切都运行时,我记录了"正在执行"消息,但随后我收到以下错误并且console.log(data)不记录.

TypeError: undefined is not a function
Run Code Online (Sandbox Code Playgroud)

通常这种类型的错误是自我解释的,所以要么我是一个白痴,要么就是令人困惑的事情.据我所见,这是一个功能.

javascript function angularjs angularjs-service angular-promise

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

AngularFire $使用Firebase参考中的变量从Array中删除项目不起作用

我一直在努力解决以下问题:

我正在尝试使用$ remove AngularFire方法从Firebase数组中删除"发布"项目,该方法已在Angular Service(Factory)中实现.这个帖子是'事件'的孩子,所以为了删除它,我必须将此服务传递给我想要删除帖子的相关事件的参数.

这是我的控制器:

app.controller('EventSignupController', function ($scope, $routeParams, EventService, AuthService) {
  // Load the selected event with firebase through the eventservice
  $scope.selectedEvent = EventService.events.get($routeParams.eventId);

  // get user settings
  $scope.user =  AuthService.user;
  $scope.signedIn = AuthService.signedIn;

  // Message functionality
  $scope.posts = EventService.posts.all($scope.selectedEvent.$id);

  $scope.post = {
    message: ''
  };
  $scope.addPost = function (){
    $scope.post.creator = $scope.user.profile.username;
    $scope.post.creatorUID = $scope.user.uid;
    EventService.posts.createPost($scope.selectedEvent.$id, $scope.post);
  };

  $scope.deletePost = function(post){
    EventService.posts.deletePost($scope.selectedEvent.$id, post);
    // workaround for eventService bug:
    // $scope.posts.$remove(post);
  };
});
Run Code Online (Sandbox Code Playgroud)

这是我的服务(工厂):

app.factory('EventService', function ($firebase, FIREBASE_URL) …
Run Code Online (Sandbox Code Playgroud)

angularjs firebase angularjs-service angularfire

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

AngularJS中的服务无法正常工作

我正在研究最简单的基于AngularJS app的方法来理解它的流程.我试图将简单的字符串从服务传递到控制器,并从那里我想在index.html中打印此字符串.似乎我的其余代码工作但没有服务,不知道我在这里缺少什么...

的index.html

<!DOCTYPE html>
<html ng-app="angularFormsApp">
 <head>
 <title>Angular Form</title>
 <meta charset="utf-8" />
  <link href="Content/bootstrap.min.css" rel="stylesheet" />
  <script src="Scripts/angular.min.js"></script>
  <script src="App/AngularFormsApp.js"></script>
  <script src="App/EmployeeForm/efService.js"></script>
  <script src="App/EmployeeForm/efController.js"></script>

<!--<script src="App/EmployeeForm/efDirective.js"></script>
-->
</head>
  <body>
     <h1>Test AngularJS App Approach</h1>

   <div ng-controller="efController">
       {{employee}}
   </div>

</body >
</html>
Run Code Online (Sandbox Code Playgroud)

AngularFormsApp.JS(模块)

var angularFormsModule = angular.module('angularFormsApp', []);
Run Code Online (Sandbox Code Playgroud)

efController.js(控制器)

angularFormsModule.controller('efController',
  function efController($scope, efService) {

    $scope.employee = efService.employee;
});
Run Code Online (Sandbox Code Playgroud)

efService.js(服务)

angularFormsModule.factory('efService',
 function () {
    var employee = "Employee Detail Available";

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

javascript angularjs angularjs-service

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

AngularJS:将文本框值传递给服务

我是angularJS的新手.目前正致力于服务.我试图将文本框值传递给服务.我不知道怎么过.我编码,但它不起作用.

app.service("wishService", function ($timeout) {
    this.wishHim = function (_wishMessage) {
        $timeout(function () {
            alert('Hi ,' + _wishMessage);
        }, 2000);
    }
});

app.controller('wishCtrl', ['$scope', function ($scope, wishService) {    
    $scope.Message = "";
    $scope.SendMessage = function (wishMessage) {
        wishService.wishHim(wishMessage);
    };
}]);


<div ng-controller="wishCtrl">
        Wish Message: <input type="text" ng-model="wishMessage" />
        <input type="button" value="Send Message" ng-click="SendMessage(wishMessage)" /><br />
        Your Message : {{Message}}
</div>
Run Code Online (Sandbox Code Playgroud)

DEMO

html angularjs angularjs-service

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

是否为angularjs中的每个控制器创建了一个新的服务实例?

我正在构建一个angularjs应用程序,它有各种控制器.对于两个控制器,我依赖注入相同的服务.是为每个注入它的控制器创建的服务的单独实例吗?

angularjs angularjs-service angularjs-factory

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