我是打字稿和MVC的新手,所以我甚至不确定我是否提出了正确的问题。但我有功能相同的打字稿文件,用于多个搜索屏幕。因此,我尝试将其整合到单个文件中,该文件采用搜索屏幕名称和区域(字符串)并从那里起飞。有没有办法可以从视图中将这些信息传递给打字稿文件?
这是我的上下文打字稿的一小段代码。
module HarSearch {
const area = "";
const searchScreen = "";
const gridName = searchScreen + "SearchGrid";
const searchGrid = $(`#${gridName}`);
const gridOptionsStorageName = searchScreen + "GridOptions";
const filtersStorageName = searchScreen + "SearchFilters";
const filterFormName = "formFilters";
var defaultGridOptions: kendo.ui.GridOptions;
$(function(e) {
var harSearchGridData = searchGrid.data("kendoGrid");
KendoGridFunctions.resizeGrid(searchGrid, -25);
defaultGridOptions = harSearchGridData.getOptions();
loadSearchState();
});
// more functions...
}
区域和searchName值是我希望能够设置的值。
编辑:考虑下面的Paleo建议,我想也许只能打破我需要的变量,自己的课可能会让我更接近。所以我对我的代码进行了一些编辑,但我仍然需要一种方法来设置这些变量。
class definitions {
public static area = "";
public static searchName = "";
}
module HarSearch {
const area = definitions.area;
const searchName = definitions.searchName;
.
.
.
// rest is the same
}
答案 0 :(得分:0)
这就是我最终做的事情。这不是我理想的解决方案,但它确实有效。我最终没有把我的变量分成他们自己的类。他们仍在模块内。我在视图中添加了一个脚本来设置全局变量,然后由模块引用,其中我的Model.SearchArea和Model.SearchName只是字符串。
查看强>
<script type="text/javascript">
$(function() {
searchArea = @Html.Raw(Json.Encode(Model.SearchArea));
searchName = @Html.Raw(Json.Encode(Model.SearchName));
})
</script>
TS文件
module Search {
declare var searchArea: string;
declare var searchName: string;
var gridName: string;
var searchGrid: JQuery;
var gridOptionsStorageName: string;
var filtersStorageName: string;
const filterFormName = "formFilters";
var defaultGridOptions: kendo.ui.GridOptions;
$(() => {
gridName = searchName + "SearchGrid";
searchGrid = $(`#${gridName}`);
gridOptionsStorageName = searchName + "GridOptions";
filtersStorageName = searchName + "SearchFilters";
var searchGridData = searchGrid.data("kendoGrid");
KendoGridFunctions.resizeGrid(searchGrid, -25);
defaultGridOptions = searchGridData.getOptions();
loadSearchState();
});
.
.
.
//more functions
}