Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

angularjs - Angular can't find the variable: data

I've already done with my first angular app. I have an error while calling a function. Here is a snippet of my JSON:

{

  "variantA": {
    "sumInsuredThirty": [
      {
        "dayFrom": 1,
        "dayTo": 3,
        "tarif": 2
      }, ...

I've got it via:

  $http.get("/CalculatorMed/JSON/rates.json/").then(function(data) {
        $scope.rates = data.data;
        });

Now, I'm trying to get the tarif:

$scope.getBaseTarif = function () {
        var baseTarif = 0;
        if (data.pickedOptions.variantA === true && data.pickedOptions.sumInsured === 30000) {
            for (var i = 0; i < rates.variantA.sumInsuredThirty.lenght; i++) {
                if (data.pickedOptions.days >= rates.variantA.sumInsuredThirty[ i ].dayFrom && data.pickedOptions.days <= rates.variantA.sumInsuredThirty[ i ].dayTo) {
                    baseTarif = rates.variantA.sumInsuredThirty[ i ].tarif;
                    return baseTarif;
                }
            }
        }
      };

And I have an error:

Error: Can't find variable: data getBaseTarif@http://localhost:63342/CalculatorMed/controller/calculator.js:34:17 fn

Many thanks in advance!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

data cannot be access outside, it should be $scope.rates,

 if ($scope.rates.pickedOptions.variantA === true && $scope.rates.pickedOptions.sumInsured === 30000) {
            for (var i = 0; i < rates.variantA.sumInsuredThirty.length; i++) {
                if ($scope.rates.pickedOptions.days >= rates.variantA.sumInsuredThirty[i].dayFrom && $scope.rates.pickedOptions.days <= rates.variantA.sumInsuredThirty[i].dayTo) {
                    baseTarif = rates.variantA.sumInsuredThirty[i].tarif;
                    return baseTarif;
                }
            }
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...