【发布时间】:2016-08-11 18:55:37
【问题描述】:
在我的应用程序中,我将返回下面使用此 TestViewModel 的视图。
public class TestViewModel
{
public List<string> Tests { get; set; }
}
查看:
@model AdminSite.Models.TestsViewModel
@{
ViewBag.Title = "Tests";
}
<hgroup class="title">
<h1>@ViewBag.Title</h1>
</hgroup>
<!doctype html>
<html>
<head>
<script src="~/Scripts/Angular/angular.min.js"></script>
<script src="~/Scripts/Angular/Tests.js"></script>
</head>
<body>
<div ng-app="testsTable">
<div ng-controller="TableController">
<table my-table options="options"></table>
</div>
</div>
</body>
</html>
如您所见,我正在使用 AngularJS 创建一个 DataTable,但我希望它不是硬编码的表数据“aaData”,而是来自 TestViewModel 模型。
var app = angular.module('testsTable', []);
app.directive('myTable', function () {
return {
restrict: 'A',
link: function (scope, element, attrs, controller) {
var dataTable = element.dataTable(scope.options);
},
scope: {
options: "="
}
};
});
app.controller('TableController', ['$scope', function ($scope) {
$scope.options = {
aoColumns: [
{
"sTitle": "Col 1",
},
{
"sTitle": "Col 2"
},
{
"sTitle": "Col 3"
}
],
aoColumnDefs: [{
"bSortable": true,
}],
bJQueryUI: true,
bDestroy: true,
aaData: [
["A", "B", "C"],
["A", "B", "C"],
]
};
}]);
我想我可能需要创建另一个指令来绑定模型,例如
<table my-table options="options" model-data="@Model.Tests"></table>
但我不确定 Angular 指令是如何准确工作的,我将如何编写所述指令以及它如何将其绑定到范围
【问题讨论】:
标签: angularjs asp.net-mvc angularjs-directive