2017-04-05 85 views
0

解析URL PARAM我有工作这样的代码Angularjs:通过服务器响应

app.ts

function getYear(): number { 
    const date = new Date(); 
    let year = date.getFullYear(); 
} 


$urlRouterProvider.when("/", `/year/${getYear()}`); 

$stateProvider.state("/", { 
     url: "/" 
    }).state("app", { 
     url: "/year/{year:int}", 
     controller: "AppController as ctrl", 
     templateUrl: "templates/application.html", 
     requireADLogin: true, 
     params: { 
      year: getYear() 
     } 
    }) 

包子现在它该电源线,以从服务器获取的 '年' PARAM。

我一直试图做这样的事情:

$stateProvider.state("/", { 
     url: "/" 
    }).state("app", { 
     url: "/year/{year:int}", 
     controller: "AppController as ctrl", 
     templateUrl: "templates/application.html", 
     requireADLogin: true,   
     resolve: { 
      year: ["YearService", (yearService: services.YearService) => { 
        return yearService.GetYear(); 
        }] 
     } 
    }) 

但这只是挂在页面守望例外anormous量。

此外,我在想:

$stateProvider.state("/", { 
     url: "/" 
    }).state("app", { 
     url: "/year/{year:int}", 
     controller: "AppController as ctrl", 
     templateUrl: "templates/application.html", 
     requireADLogin: true, 
     params: { 
      year: function(){ 
       //Here is the question 
       //How to resolve services.YearService? 
      } 
     } 
    }) 
+0

可以试试你的PARAMS这样的.. PARAMS:{ 年:{类型:“诠释”,原料:真正}} HTTPS –

+0

: //ui-router.github.io/ng1/docs/latest/interfaces/params.paramdeclaration.html –

回答

0

我结束了syncronous服务器调用替换getYear()功能是这样的:

function getYear(): number { 
     if (!window["initialYear"]) { 
      var year = $.ajax({ 
       type: "GET", 
       url: "api/year/initial", 
       async: false 
      }).responseText; 

      window["initialYear"] = parseInt(year); 
     } 

     return window["initialYear"]; 
    } 

这是不是最好的方式,因为它是syncronous并停止角处理提出请求。

这是更好地使用index.html文件与<script/>参考您的API方法应与被放置在index.html文件上面<script/>标签与API调用回调函数返回JSONP。而在回调函数需要初始化window["initialYear"]这将是然后在app.ts