Angularjs控制器本地错误
Angularjs controller Error Native
我正在开发一个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.app
或content
部分中有什么?
来自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
相关文章:
- 从我的控制器返回一个不同于200的代码以触发ajax错误,这被认为是一种好的做法吗
- 错误405:向Java控制器(Ajax)发送JSON时找不到POST方法
- Angular JS控制器初始化错误
- Angularjs控制器本地错误
- 调用控制器后Ajax抛出错误
- AngularJS控制器在注入工厂时抛出错误
- Angular JS/Rube-on-Rails-控制器未识别错误
- AngularJs 错误:$http请求虽然在控制器中定义
- 缺少 Angular JS 必需控制器错误:找不到指令所需的控制器“ngModel”
- 在Emberjs中设置从路由到控制器的数据错误
- 操作映射触发控制器中的错误方法
- 弹簧控制器未命中.正在转到错误块
- 控制器错误:参数不是函数,未定义
- 错误:Error:areq定义控制器时参数错误
- 错误:[ng:areq]参数'控制器名称'不是函数,未定义
- AngularJS“控制器作为”语法错误
- 一个控制器中的多个路由会导致 400:错误请求
- AngularJS错误 - “控制器未定义”或“未定义函数”
- angularJS显示空白页面没有错误-控制器问题
- AngularJS-错误控制器不是函数,未定义