在angularjs中调用控制器内的服务

时间:2016-04-18 16:29:22

标签: angularjs

我正在使用ng-resource进行CRUD操作,如下所示:

app
.factory('candidatureService',['$resource',
  function ($resource) {
    return $resource('http://localhost:8080/candidature/candidats', {}, {
      update: { method: "PUT"}
    });

  }]);

在我的情况下我只需要PUT。

在控制器中我有这个:

app
  .controller('CandidatureStartCtrl', function ($scope, candidatureService) {


    $scope.candidature = {};
    $scope.candidature.guarantor = {};

    $scope.submitForm1 = function(isValid) {
      console.log('validate form');

      // check to make sure the form is completely valid
      if (isValid) {
        console.log('form 1 is amazing');
        console.log($scope.user);
        candidatureService.update({},{
          "username": "Uchiha",
          "nom": $scope.candidature.lastname,
          "prenom": $scope.candidature.firstname,
          "nomarab": $scope.candidature.arabicLastname,
          "prenomarab": $scope.candidature.arabicFirstname,
          "genre": $scope.candidature.gender,
          "dateNaissance": $scope.candidature.birthdate,
          "email": $scope.candidature.email,
          "telephone": $scope.candidature.phoneNumber,
          "photo": $scope.candidature.photo,
          "cin": $scope.candidature.identityDocumentCode,
          "adresse": $scope.candidature.address,
          "codeMassar": $scope.candidature.codeMassar,
          "lieuNaissance": $scope.candidature.placeOfBirth,
          "cinPhoto": $scope.candidature.identityDocument,
          "anneebac": $scope.candidature.anneebac,
          "nomGarant": $scope.candidature.guarantor.fullName,
          "lienGarant": $scope.candidature.guarantor.link,
          "telephoneGarant": $scope.candidature.guarantor.phoneNumber,
          "province": {
            "codeProvince" : $scope.candidature.province
          }
        });
        $scope.steps.step2=true;
      } else {
        console.log('form is invalid');
      }

    };

  });

username是我案例中的主键。

但是当我使用此控制器时,我收到此错误消息:

TypeError: candidatureService.update is not a function
    at new <anonymous> (http://localhost:8080/app/scripts/controllers/candidature-start.js:106:24)

在我的index.html中,我已将控制器和服务声明为:

<script src="scripts/controllers/candidature-start.js"></script>
<script src="scripts/service/candidatureService.js"></script>

当我访问我的网页时,我会调用'CandidatureStartCtrl'中声明的candidature-start.js

.state('candidature.start', {
        url: '/start',
        controller : 'CandidatureStartCtrl',
        templateUrl: 'views/tmpl/frontend/candidature-start.html',
        containerClass: 'hz-menu',
        resolve: {
          plugins: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load([
              'scripts/vendor/slider/bootstrap-slider.js',
              'scripts/vendor/touchspin/jquery.bootstrap-touchspin.js',
              'scripts/vendor/touchspin/jquery.bootstrap-touchspin.css',
              'scripts/vendor/filestyle/bootstrap-filestyle.min.js'
            ]);
          }]
        }
      })

那么为什么我收到错误信息需要3个小时,但我无法弄清楚问题是什么!

1 个答案:

答案 0 :(得分:0)

当您使用$ resource时,服务中的方法名称前缀为$,因此调用应该是candidatureService。$ update