我可以在控制器中调用多个java脚本函数吗?

时间:2017-02-13 08:51:15

标签: javascript angularjs

我使用自定义服务定义了一个JavaScript函数,我使用控制器中的服务调用了此函数。这个函数使用两个参数:第一个是我通过点击下面的API获得的输入,第二个是我使用ng-model指令得到的年份的值。当我在控制器中调用此函数时,我收到类似type is not definedid is not defined等错误。是否是在控制器中调用JavaScript函数的正确方法。请建议我。

$http.get("http://152.144.218.70:8080/USACrime/api/crimeMultiple?city="  +$scope.strCity + "&crime=" + $scope.type1 + "&model=" + model).success(function (result) {
    $scope.prograssing = false;
    console.log("manisha", $scope.strCity);
    console.log("kanika", result);
    $scope.output = result;
    console.log("monga", $scope.output);
    $scope.hex = hexafy.year_city($scope.output,$scope.type);
    console.log("service", $scope.hex);
});

myapp.js

var app= angular.module("myApp",["ngRoute","leaflet-directive","pb.ds.components"]);
var geomarker = new L.FeatureGroup();
app.service('hexafy', function() {
    this.year_city = function (input2,years) {
        if(years.toLowerCase()=="all"){
        years = "2012,2013,2014,2015,2016,2017,2018,2019";
    }
    var yrs = years.split(",");
    output = {};
    outerBoundary = {};
    boundary = {};
    boundary["boundaryId"] = input[0]["id"];
    boundary["boundaryType"] = input[0]["type"];
    boundary["boundaryRef"] = "C1";
    outerBoundary["boundary"] = boundary;
    output["boundaries"] =outerBoundary;
    themes = [];
    for(var i in input){
        crimeTheme = {};
        crimeThemeValue = {};
        crimeThemeValue["boundaryRef"] = "C1";
        result = [];
        for(var j in input[i]["prediction"]){
            dict = {};
            if(yrs.indexOf(input[i]["prediction"][j]["year"])>-1){
                dict["name"] = input[i]["prediction"][j]["year"]+" "+input[i]["crime"]+" Crime";
                dict["description"] = input[i]["crime"]+" Crime for "+input[i]["prediction"][j]["year"];
                dict["value"] = input[i]["prediction"][j]["count"];
                dict["accuracy"] = input[i]["accuracy"];
                result.push(dict);
            }
        }
        crime = input[i]["crime"].toLowerCase()+"CrimeTheme";
        crimeThemeValue["individualValueVariable"] = result;
        console.log('crimeThemeValue["individualValueVariable"]',crimeThemeValue["individualValueVariable"]);
        crimeTheme[crime] = crimeThemeValue;
        themes.push(crimeTheme);
        console.log("themes",JSON.stringify(themes));
    }
    output["themes"] = themes;
    console.log(output);
    return output;
    };
});
});

1 个答案:

答案 0 :(得分:0)

1){@ 1}}和.success方法已被弃用,并不适合使用它。相反,你最好使用.error

2)要使用服务方法,正确的方法是这样:

.then(successCallback, errorCallback)

所以在你的情况下,走的路是:

app.service('myService', function() {
  var service = {
    method:method
  };

  return service;

  function method() {
   //Logic
  }

})