2017-04-05 66 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