angular.js - 关于angularjs中调用服务的方法的问题
世界只因有你
世界只因有你 2017-05-15 17:04:08
[AngularJS讨论组]

我想提交页面的表单数据,通过ng-submit来调用controller中注入的service的方法。但是请求的url不正确。没有调用到正确的service

页面如下:





    
    
    Kindle Binding


























controller.js如下:

angular.module('binding',['binding.services'])
.controller('formController',function($scope,bindingDataService){
    var params = $scope.bindingData;
    console.log('params: '+params);
    $scope.submitForm = bindingDataService.bindingData('bindingData',params);
});

service.js如下:

angular.module('binding.services',[])
.factory('bindingDataService', function($http){

     console.log('entered the service')
     var bindingData = function(action, params){
         return $http({
                 method: 'POST',
                 url: 'http://89e812c3.ngrok.io/KindlePocket/'+action+'',
                 // pass in data as strings
                 data: $.param(params),
                 headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
             }).success(function(data,status){
                 console.log("success! status:"+status);
                 alert("binding successfully!")
             }).error(function(data,status){
                 alert("binding error!")
                 console.log("error! status:"+status);
             })
     }

     return {
        bindingData: function(action,params){
         console.log('entered the function')
            return bindingData(action,params);
        }
     }

});

每次点击button总是会请求到跳转到这个页面的url。
请问是什么原因呢? 之前是把请求相关的代码直接放在controller中,没有问题。是service注入的不对么? 谢谢大家!

世界只因有你
世界只因有你

全部回复(2)
習慣沉默

自问自答,修改controller代码如下:

angular.module('binding',['binding.services'])
.controller('formController',function($scope,bindingDataService){
    $scope.bindingData = {};
    var params = $scope.bindingData;
    console.log('params: '+params);

    $scope.submitForm = function(){
        bindingDataService.binding('bindingData',params);
    }
  
});
迷茫

太长了,ng-submit的用法怎么用的?

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号