angular js modal popup调用工厂函数

时间:2014-07-25 06:32:59

标签: javascript angularjs modal-dialog

我试图在我的模态弹出窗口中调用工厂函数,但它给出了错误

这是我的工厂

 .factory('PartnerFactory', ['$http', '$rootScope', function ($http, $rootScope) {
             var currentPartner;
             var partners = [];
             var currentClient;
             var currentSite
             return {

             savePartner: function (partnerData) {
                 console.log("In Save Partner");
                 console.log(partnerData);

                 var url = "/api/Partner/CreatePartner";

                 var dataToPost = { Name: partnerData.Name, Description: partnerData.Description, Address1: partnerData.Address1 }
                 ////$("#spinner").show();

                 $http.post(url, dataToPost)
                 .success(function (data) {


                 })
                  .error(function (data) {

                      // return $scope.showInfoOnSubmit = !0, $scope.revert()
                  })


             },

             getPartners: function () {
                 var url = "/api/Partner/GetAllPartners";
                 return $http.get(url).then(function (response) {
                     partners = response.data;
                     $rootScope.$broadcast('handleSharedPartners', partners);
                     return partners;
                 })
             }


         };
     }])

模态对话框

 .controller("PartnerModalCtrl", ["$scope","$http", "$modalInstance", "items", '$routeParams','PartnerFactory', function ($scope, $http, $modalInstance,$routeParams, PartnerFactory) {

            $scope.test = function (partnerData) {
                console.log("Test");
                console.log(partnerData);

                return this.PartnerFactory.currentPartner();
            },
             $scope.ok = function () { $modalInstance.close(); },
          $scope.cancel = function () { $modalInstance.dismiss("cancel") },
            $scope.addPartner = function (partnerData) {

                console.log(partnerData);

           return   PartnerFactory.savePartner(partnerData);
            };




        }])

我收到错误,因为addPartner不是定义的函数

1 个答案:

答案 0 :(得分:0)

您在工厂和控制器中都遇到问题,已经纠正并粘贴了以下代码:

厂:

         savePartner: function (partnerData) {
             console.log("In Save Partner");
             console.log(partnerData);

             var url = "/api/Partner/CreatePartner";

             var dataToPost = { Name: partnerData.Name, Description: partnerData.Description, Address1: partnerData.Address1 }
             ////$("#spinner").show();

             $http.post(url, dataToPost)
             .success(function (data) {
                   //need to return the data from the function 
                   return data;
             })
              .error(function (data) {

                  // return $scope.showInfoOnSubmit = !0, $scope.revert()
              })


         },

         getPartners: function () {
             var url = "/api/Partner/GetAllPartners";
             return $http.get(url).then(function (response) {
                 partners = response.data;
                 $rootScope.$broadcast('handleSharedPartners', partners);
                 return partners;
             })
         }


     };
 }])

控制器:

 .controller("PartnerModalCtrl", ["$scope","$http", "$modalInstance", "items", '$routeParams','PartnerFactory', function ($scope, $http, $modalInstance,items,$routeParams, PartnerFactory) {

        $scope.test = function (partnerData) {
            console.log("Test");
            console.log(partnerData);

            return this.PartnerFactory.currentPartner();
        },
         $scope.ok = function () { $modalInstance.close(); }

      $scope.cancel = function () { $modalInstance.dismiss("cancel") }

        $scope.addPartner = function (partnerData) {
            console.log(partnerData);
            return   PartnerFactory.savePartner(partnerData);
        };
    }])

如果问题仍然存在,请创建一个小提琴,以便我们可以查看完整的代码