簡體   English   中英

我可以將參數從控制器傳遞到app.js嗎?

[英]Can i pass a parameter from controller to app.js?

如果變量為true,我需要顯示離子應用程序的開始視圖,如果變量為false,則需要顯示另一個視圖,所以我需要知道是否可以從控制器獲取參數並在app.js中使用它:

 if (parameter===true) { $urlRouterProvider.otherwise('/app/inicio'); }else { $urlRouterProvider.otherwise('/app/inicio2'); } 

有什么建議么?

對於供應商來說,這個答案可能不是最好的用法,但是它將為您提供所需的東西。

Ionic應用程序狀態在模塊config塊中定義,並且在config塊中沒有控制器,服務,工廠,實例化。 在配置塊中實例化的唯一角度對象(常量旁邊)是提供者。 因此,您可以創建一個自定義提供程序,該提供程序可以為您提供參數,該參數將告訴您應用程序啟動時向用戶顯示哪個視圖。 這是創建提供者的方法:

app.provider('mainView', function MainViewProvider() {
  var someCondition = false;

  this.getCurrentMainView = function() {
    if(someCondition)
      return "/app/inicio";
    else {
      return "/app/inicio2";
    }
  };
  //return a dummy factory that will never be used or injected
  this.$get = ["nullService", function(){return null;}];

});

然后在您的配置塊中,注入提供程序並使用getCurrentView函數獲取URL來導航到

app.config(function($stateProvider, $urlRouterProvider, mainViewProvider) {
  $urlRouterProvider.otherwise(mainViewProvider.getCurrentMainView());

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM