简体   繁体   中英

AngularJS Uncaught ReferenceError: controller is not defined

I'm trying to prepare the infrastructure needed to record the data in base using Web Api... And my question is why is getting this error:

ReferenceError: WebApi is not defined
at Scope.$scope.salvar (Controller.js:22)
at fn (eval at compile (angular.js:15500), :4:138)
at callback (angular.js:27285)
at Scope.$eval (angular.js:18372)
at Scope.$apply (angular.js:18472)
at HTMLButtonElement. (angular.js:27290)
at HTMLButtonElement.dispatch (jquery-3.2.1.js:5206)
at HTMLButtonElement.elemData.handle (jquery-3.2.1.js:5014)

My code is:

Controller.js

prontuariogestanteapp.controller("ProntuariogCtrl", ["$scope", "CalculadorIMC", "WebApi", function ($scope, calculadorIMC, WebApi) 
{
    $scope.tabs = [
        { Title: "Dados Básicos", PageToDisplay: "./Html/Dados_Basicos.html" },
        { Title: "1º Mês", PageToDisplay: "./Html/Mes_1.html" },
        { Title: "2º Mês", PageToDisplay: "./Html/Mes_2.html" },
        { Title: "3º Mês", PageToDisplay: "./Html/Mes_3.html" },
        { Title: "4º Mês", PageToDisplay: "./Html/Mes_4.html" },
        { Title: "5º Mês", PageToDisplay: "./Html/Mes_5.html" },
        { Title: "6º Mês", PageToDisplay: "./Html/Mes_6.html" },
        { Title: "7º Mês", PageToDisplay: "./Html/Mes_7.html" },
        { Title: "8º Mês", PageToDisplay: "./Html/Mes_8.html" },
        { Title: "9º Mês", PageToDisplay: "./Html/Mes_9.html" },
        { Title: "Pós-Parto", PageToDisplay: "./Html/Mes10_Pos_Parto.html" }
    ];

    $scope.salvar = function()
    {
        WebApi.salvar($scope.Mes1);
    };
}

Change

$scope.salvar = function WebApi()
{
    WebApi.salvar($scope.Mes1);

};

to

$scope.salvar = function()
{
    WebApi.salvar($scope.Mes1);

};

You can't name a method twice ($scope.salvar and WebApi).

After you resolve @Jasper Seinhorst suggestion you must check the following:

  1. The js file where WebApi service is included properly in your index.html
  2. Your WebApi service is stored inside a module; ensure that module is a dependency for the module where you want to use it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM