我无法理解将json数据导入应用程序时遇到的问题。我在控制台工具中看不到任何调用,我不断收到$ injector:unpr错误。我已经在controller.js
中编写了这段代码 function caderror($scope, SampleData) {
// Run method getFullList() from factory
SampleData.getFullList().success(function(data){
// Assign data to $scope
$scope.dataFromFactory = data;
});
}
这里是样本数据所在的factories.js。
function SampleData($http) {
// Return the object
return {
// Create simple method to get data from $http service
getFullList : function() {
return $http({
url: 'http://somewebsite/api/example.json',
method: 'GET'
})
}
}
}
angular
.module('inspinia')
.factory('SampleData', SampleData);
以下是我用来将JSON数据调用到表中的HTML
<div class="row wrapper border-bottom white-bg page-heading">
<div class="col-lg-10">
<h2>CAD Error Logs</h2>
<ol class="breadcrumb">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a>Tables</a>
</li>
<li class="active">
<strong>Data Tables</strong>
</li>
</ol>
</div>
<div class="col-lg-2">
</div>
</div>
<div class="wrapper wrapper-content animated fadeInRight">
<div class="row">
<div class="col-lg-12">
<div class="text-center m-t-lg">
<h1>
Simple factory to get data from server
</h1>
</div>
</div>
<div class="col-lg-6 col-lg-offset-3">
<div class="ibox">
<div class="ibox-content">
<p class="text-center">
Table with data from api/example.json
</p>
<table class="table table-striped">
<thead>
<tr>
<th>
Id
</th>
<th>
Name
</th>
<th>
Company
</th>
<th>
Description
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in dataFromFactory">
<td>
{{item.id}}
</td>
<td>
{{item.name}}
</td>
<td>
{{item.company}}
</td>
<td>
{{item.desc}}
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
我做错了什么,如果需要更多代码,请告诉我,我会发布。我读到了错误,它说它是由于无法解决所需的依赖关系造成的。我该如何解决这个问题。角js的错误网站并没有真正帮助我。
修改
以下是错误消息
错误:[$ injector:unpr] http://errors.angularjs.org/1.5.0/$injector/unpr?p0=SampleDataProvider%20%3C-%20SampleData%20%3C-“aderror
我可以展示其余部分,但它们只是angular.min.js中的文件行
答案 0 :(得分:1)
由于您的<script>
引用存在问题,通常会发生此错误。
错误告诉我们函数caderror
正在查找SampleData
但找不到它。最常见的情况是,包含SampleData
代码的文件未包含在<script>
引用中,或者在之后列出需要它的文件(带{{的文件) 1}}在其中,在这种情况下)。
此外,您应该考虑在方法中不使用caderror
,因为它已被弃用。见Why are angular $http success/error methods deprecated? Removed from v1.6?