Angularjs控制器本地错误

Angularjs controller Error Native

本文关键字:错误 控制器 Angularjs      更新时间:2023-09-26

我正在开发一个web应用程序,我使用Laravel作为REST Api的后端,使用angular作为前端。当我在浏览器中启动应用程序时,我收到以下错误。

错误:[ng:areq]http://errors.angularjs.org/1.5.5/ng/areq?p0=mainController&p1=不是%20a%20函数%2C%20得到%20未定义错误(本机)

App.js

var myApp = angular.module('myApp', ['mainCtrl', 'myAppService']);

MyAppService.js

angular.module('myAppService', [])
    .factory('Result', function($http) {
        return {
            get : function() {
                return $http.get('http://localhost/ngresulty/public/result');
            },
            show : function(id) {
                return $http.get('api/result/' + id);
            },
            save : function(resultData) {
                return $http({
                    method: 'POST',
                    url: 'api/result',
                    headers: { 'Content-Type' : 'application/x-www-form-urlencoded' },
                    data: $.param(resultData)
                });
            },
            destroy : function(id) {
                return $http.delete('api/result/' + id);
            }
        }
    });

MainCtrl.js

angular.module('mainCtrl', [])
    .controller('mainController', function($scope, $http, Result) {
        // object to hold all the data for the new comment form
        $scope.resultData = {};
        // loading variable to show the spinning loading icon
        $scope.loading = true;
        // get all the comments first and bind it to the $scope.comments object
        Result.get()
            .success(function(data) {
                $scope.students = data;
                $scope.loading = false;
            });

        // function to handle submitting the form
        $scope.submitResult = function() {
            $scope.loading = true;
            // save the comment. pass in comment data from the form
            Result.save($scope.resultData)
                .success(function(data) {
                    $scope.resultData = {};
                    // if successful, we'll need to refresh the comment list
                    Result.get()
                        .success(function(getData) {
                            $scope.students = getData;
                            $scope.loading = false;
                        });
                })
                .error(function(data) {
                    console.log(data);
                });
        };
        // function to handle deleting a comment
        $scope.deleteResult = function(id) {
            $scope.loading = true; 
            Result.destroy(id)
                .success(function(data) {
                    // if successful, we'll need to refresh the comment list
                    Result.get()
                        .success(function(getData) {
                            $scope.students = getData;
                            $scope.loading = false;
                        });
                });
        };
    });

查看或结果.blade.php

@extends('layouts.app')
@section('content')

<div class="container" ng-app="myApp" ng-controller="mainController">
    <div class="row">
        <div class="col-md-8">
            <div class="panel panel-default">
                <div class="panel-heading">All Students Results Record</div>
                <label>Search: <input ng-model="searchText"></label>
                <div class="panel-body">
                    <input type="text" ng-model="search">
                    <table class="table table-striped">
                            <thead>
                              <tr>
                                <th>Roll No</th>
                                <th>Student Name</th>
                              </tr>
                            </thead>
                            <tbody ng-hide="loading" ng-repeat="student in students | filter:searchText">
                                    <td>@{{ student.rollno }}</td>
                                    <td>@{{ student.name }}</td>

                            </tbody>
                    </table>
                </div>
            </div>
        </div>
        <div class="col-md-4">
        <div class="panel panel-default">
                <div class="panel-heading">Action(s)</div>
                <div class="panel-body">
                    <center><a type="submit" href="/ngresulty/public/result/create" class="btn btn-info btn-lg">Add New Result(s)</a></center>
                </div>
            </div>
        </div>
    </div>
</div>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js">
    <script src="app/controllers/mainCtrl.js"></script> <!-- load our controller -->
    <script src="app/services/myAppService.js"></script> <!-- load our service -->
    <script src="app/app.js"></script> <!-- load our application -->
@endsection

查找页面早期使用的ng-app指令。

@extends('layouts.app')
@section('content')

<div class="container" ng-app="myApp" ng-controller="mainController">

layouts.appcontent部分中有什么?

来自DOCS:

使用ngApp:时需要记住以下几点

每个HTML文档只能自动启动一个AngularJS应用程序。文档中找到的第一个ngApp将用于定义根元素,以将其自动引导为应用程序。要在HTML文档中运行多个应用程序,必须使用angular.bootstrap手动引导它们。

如果较早使用ng-app指令,则会忽略后续的ng-app="myApp",导致找不到mainController函数。

使用

angular.module('mainCtrl', [])

在控制器/服务/指令定义中创建一个新模块。

当你想参考mainCtrl模块时,做

angular.module('mainCtrl')

相反。

从Angular文档中:"注意使用Angular.module('myModule',[])将创建模块myModule并覆盖任何名为myModule的现有模块。使用Angular.module('myModule')检索现有模块。"

https://docs.angularjs.org/guide/module