发送存储在服务上的数据

时间:2016-08-05 20:50:19

标签: javascript angularjs

我有以下控制器和服务

text <-
"Name, Address, DOB
John, Manhattan, New York, 2/8/1990
Jacob, Arizona, 9/10/2012
Smith, New Jersey, 8/10/2016
"
cat(text, file = "foo", sep = ",")
fields <- count.fields("foo", sep = ",")
readLines("foo")[fields == 3]

此外,我还有一个用于发送区号和国家/地区代码的表单。

angular
    .module('myApp')
    .controller('Spliptter', Spliptter) // Controller
    .service('SplitService', SplitService); //Service

    function Spliptter($scope, SplitService){
        var result = SplitService.phoeNoSplit($scope.phoneNumber.number); //Phone Data

    $scope.area: result['area'];
    $scope.country: result['country'];
    }

    function SplitService() {
      this.phoeNoSplit = function(phoneNumber) {
         var area = phoneNumber.substring(0, 3); //Info that I want to send
         var country = phoneNumber.substring(3, 10);  //Parse

         return {
             'area': area,
             'country': country
          }
     }
}

但是,我不知道这个问题是什么。我是棱角分明的新人。

1 个答案:

答案 0 :(得分:0)

请先阅读角度文档,以便更好地了解控制器和服务的工作原理。

这里的问题是你试图将Spliptter控制器的范围访问到formController控制器,这是永远不可能的。 如果必须在formController中使用该范围值,则必须在formController本身中初始化这些范围值。

参见下面的代码:

的FormController

 angular //Controller of the form that i'm using to send the area and country code
.module('myApp')
.controller('formController', formController); // Form controller
   function formController($scope, $http, $rootScope, SplitService) {
      var result = SplitService.phoeNoSplit($scope.phoneNumber.number); 

      $scope.area: result['area'];
      $scope.country: result['country']; 

       $scope.SendFormController = function () {
            $http({
                method:'POST',
                url:myURL.com,
                data : {
                        ciaPhone: $scope.TokenResponse.datos.ciaPhone,  // am not sure from where you are getting it
                        phoneCountry: $scope.country,
                        phoneArea: $scope.area
                        }
                }, // form fields
                headers: {
                    'Content-Type': 'application/json'
                }//
            })
            .success(function ( data, status, headers) {
                $rootScope.datosPersonales = data;
            })
            .error(function (data, status, header, config) {
        }; //
 }