从typescript引用.cshtml中的全局变量集

时间:2015-06-18 18:24:51

标签: angularjs typescript typescript1.4

我在.cshtml文件中设置了一些变量,以便在我的角度服务中使用它们:

<script>
        var _APIURL = '@(Url.Content("~/Admin/api/"))';
        var _AREAURL = '@(Url.Content("~/Admin/"))';
        var _APPURL = '@(Url.Content("~/"))';
        var _AREAPATH = "@(Url.Content("~/") + "Areas/" + HttpContext.Current.Request.RequestContext.RouteData.DataTokens["area"])";
</script>  

然后,在我的模块中,我想引用它们,类似于:

((): void => {
    var app = angular.module('system', ['ngRoute', 'ui.bootstrap', 'gettext', 'ngCookies', 'ipCookie']);


    app.constant("_AREAPATH", _AREAPATH); // this value comes from the view
    app.constant("_AREAURL", _AREAURL); // this value comes from the view
    app.constant("_APPURL",  _APPURL); // this value comes from the view

    app.config(["$routeProvider", "_AREAPATH", ($routeProvider, areaPath) => {
        $routeProvider
            .when('/', { templateUrl: areaPath + "/Templates/System/System.html" })
            .otherwise({
                template: "This doesn't exist!"
            });
    }]);
})()

但是我得到了“无法解析符号_AREAURL”错误。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:3)

我认为您需要在TypeScript中添加一些环境定义:

declare var _APIURL: string;
declare var _AREAURL: string;
declare var _APPURL: string;
declare var _AREAPATH: string;