AngularJS : factory $http service

后端 未结 2 1615
一整个雨季
一整个雨季 2020-12-02 09:37

I am trying to understand the concept of factory and service in Angular. I have the following code under the controller

init();

    function init(){
                


        
2条回答
  •  天命终不由人
    2020-12-02 10:01

    The first answer is great but maybe you can understand this:

    studentApp.factory('studentSessionFactory', function($http){
        var factory = {};
    
        factory.getSessions = function(){
            return $http.post('/services', {type :'getSource',ID :'TP001'});
        };
    
        return factory;
    });
    

    Then:

    studentApp.controller('studentMenu',function($scope, studentSessionFactory){
          $scope.variableName = [];
    
          init();
    
          function init(){
              studentSessionFactory.getSessions().success(function(data, status){
                  $scope.variableName = data;
              });
              console.log($scope.variableName);
         };
     });
    

提交回复
热议问题