【发布时间】:2016-05-13 15:22:51
【问题描述】:
我正在开发一个网络应用程序,我使用 Laravel 作为后端仅用于 REST Api 和 angular 作为前端。当我在浏览器中启动我的应用程序时出现以下错误。
错误:[ng:areq] http://errors.angularjs.org/1.5.5/ng/areq?p0=mainController&p1=not%20a%20function%2C%20got%20undefined 在错误(本机)
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;
});
});
};
});
查看或 Results.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
【问题讨论】:
标签: javascript angularjs laravel