我总是收到错误" TypeError:无法读取属性'排序'未定义"尝试使用ng-tables打开页面时。我有一个控制器
movies.js
app.controller('moviesController', ['$scope', '$http', '$uibModal', 'blockUI', 'notifyService', 'lib', '$filter', 'NgTableParams', function ($scope, $http, $uibModal, blockUI, notifyService, lib, $filter, NgTableParams) {
var createTable = function (options, dataFunction) {
return new NgTableParams(options, {
total: dataFunction().length,
getData: function ($defer, params) {
var orderedData = params.sorting() ? $filter('orderBy')(dataFunction(), params.orderBy()) : dataFunction();
orderedData = params.filter() ? $filter('filter')(orderedData, params.filter()) : orderedData;
params.total(orderedData.length);
$defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});
};
var movies = [];
$scope.moviesTable = createTable({
page: 1, count: 10, sorting: {
title: 'asc'
}
}, function () {
return movies;
});
var init = function () {
loadMovies();
};
var loadMovies = function () {
blockUI.start();
$http.get('/Api/Movies').then(
function (response) {
blockUI.stop();
movies = response.data;
$scope.moviesTable.reload();
}, lib.handleError);
};
init();
}]);
它的html看起来像那样:
Index.cshtml
@using System.Web.Optimization
@{
ViewBag.Title = "Movies";
}
@section libs {
@Scripts.Render("~/bundles/movies")
}
<html>
<head>
</head>
<body>
<div ng-controller="moviesController" block-ui="moviesContentDiv">
<h2>Movies</h2>
<div ng-show="movies.length < 1">
<p>There are no registered Movies yet.</p>
</div>
<div ng-hide="moviesTable.data.length < 1">
<table ng-table="moviesTable" class="table" show-filter="true">
<tr ng-repeat="movie in $data">
<td title="'Title'" filter="{ title: 'text'}" sortable="'title'">
<a ng-href="/Movie/Details/{{movie.id}}"> {{movie.title }}</a>
</td>
<td title="'Rank'" filter="{ rank: 'number'}" sortable="'rank'">
{{movie.rank}}
</td>
<td title="'Year'" filter="{ year: 'number'}" sortable="'year'">
{{movie.year}}
</td>
<td title="'Genre'" sortable="'genre'">
{{movie.genre}}
</td>
</tr>
</table>
</div>
<div class="top-space-lg">
<a href="/Movie/Create" class="btn btn-default"><i class="fa fa-plus"></i> Add movie</a>
</div>
</div>
</body>
</html>
html有一个共享布局,其部分代码具有
_Layout.cshtml
@Scripts.Render("~/bundles/lib")
@Scripts.Render("~/bundles/angular")
@Scripts.Render("~/bundles/uiBootstrap")
在另一个文件中,我包括像:
这样的包BundleConfig.cs
bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/bootstrap.css", "~/Content/angular-block-ui.css", "~/Content/pnotify.css"
, "~/Content/ng-table.css", "~/Content/Site.css"));
bundles.Add(new ScriptBundle("~/bundles/angular").Include("~/Scripts/angular.js", "~/Scripts/angular-route.js",
"~/Scripts/angular-block-ui.js", "~/Scripts/pnotify.js", "~/Scripts/directives.js","~/Scripts/ng-table.js"));
我还是AngularJS,HTML和C#的新手。什么可能是错误的并导致该错误?我找不到什么可以解决我的问题
答案 0 :(得分:0)
我发现了。我在NuGet上安装了ngTable 3.0.1。在我卸载它并再次安装但版本为0.8.3之后,错误停止并且应用程序开始正常工作。