2014-02-19 46 views
1

我試圖用引導模式創建一個通用的yes/no模態對話框服務。但是我很難將文本數據(標題和消息)注入到模態控制器中。將對象注入引導模式

這是肯定沒有模態控制器:

module Dialogs 
{ 
export class YesNoDialog 
{ 
    static $inject = ['$scope', 'YesNoDialogConfig']; 

    Title: string; 
    Message: string; 

    constructor($scope, YesNoDialogConfig) 
    { 
     $scope.vm = this;    

     this.Title = YesNoDialogConfig.Title; 
     this.Message = YesNoDialogConfig.Message; 
    }   
} 
} 

這是對是沒有對話的服務:

module DialogServices 
{ 
export class YesNoDialogService 
{ 
    $inject = ['$modal']; 

    ModalService: ng.ui.bootstrap.IModalService; 

    constructor($modal) 
    { 
     this.ModalService = $modal; 
    } 

    ShowModal(Title: string, Message: string) : ng.IPromise<any> 
    { 
     return this.ModalService.open(
      { 
       templateUrl: 'App/Views/Dialogs/YesNoDialog.html', 
       controller: Dialogs.YesNoDialog, 
       resolve: { 
        YesNoDialogConfig: 
        { 
         Title: Title, 
         Message: Message 
        } 
       } 

      }).result; 
    } 
} 
} 

但沒有工作,YesNoDialogConfig在模式的構造是未定義。我曾嘗試其他的事情,我試圖創建一個YesNoDialogConfig類,填充它,然後調用它通過:

ShowModal(Title: string, Message: string) : ng.IPromise<any> 
    { 
     this.ModalConfig = new Models.YesNoDialogConfig(Title, Message); 

     return this.ModalService.open(
      { 
       templateUrl: 'App/Views/Dialogs/YesNoDialog.html', 
       controller: Dialogs.YesNoDialog, 
       resolve: { 
        YesNoDialogConfig: function() 
        { 
         { return this.ModalConfig } 
        } 

      }).result; 
    } 

但是,要麼沒有工作。有什麼建議麼?謝謝!

+0

看看這...也許這將有助於你,它支持各種變量傳遞到模態 - https://github.com/doodeec/dcom-angular-dialog – doodeec

+0

感謝您的建議。如果我不能用自己的對話服務獲勝,我會用它,但我現在正在忙於學習打字稿/角度/引導,所以我寧願在這個時間點推出自己的。 – user1435899

回答

2

好吧,事實證明,我缺乏知識讓我在那裏。我做了什麼是attemped注入混凝土是沒有配置類成模態控制器:

export class YesNoDialog 
{ 
    static $inject = ['$scope', 'YesNoDialogConfig']; 

    Title: string; 
    Message: string;   

    constructor($scope, YesNoDialogConfig: Dialogs.YesNoDialogConfig) 
    { 
     $scope.vm = this;    

     this.Title = YesNoDialogConfig.Title; 
     this.Message = YesNoDialogConfig.Message; 
    } 
} 

實際是沒有配置類:

export class YesNoDialogConfig 
{ 
    Title: string; 
    Message: string; 

    constructor(Title: string, Message: string) 
    { 
     this.Title = Title; 
     this.Message = Message; 
    } 
} 

,當然還有,這是不行的,所以我所做的,而是創造了一個肯定的沒有配置接口和注射的是,在隨後的方法調用模式對話框,只是解決了具體類的新實例:

修改後的控制器:

export class YesNoDialog 
{ 
    static $inject = ['$scope', 'YesNoDialogConfig']; 

    Title: string; 
    Message: string;   

    constructor($scope, YesNoDialogConfig: Dialogs.IYesNoDialogConfig) 
    { 
     $scope.vm = this;   

     this.Title = YesNoDialogConfig.Title; 
     this.Message = YesNoDialogConfig.Message; 
    } 
} 

新界面:

export interface IYesNoDialogConfig 
{ 
    Title: string; 
    Message: string; 
} 

export class YesNoDialogConfig implements IYesNoDialogConfig 
{ 
    Title: string; 
    Message: string; 

    constructor(Title: string, Message: string) 
    { 
     this.Title = Title; 
     this.Message = Message; 
    } 
} 

從服務電話:

return this.ModalService.open(
      { 
       templateUrl: 'App/Views/Dialogs/YesNoDialog.html', 
       controller: Dialogs.YesNoDialog, 
       resolve: { 
        YesNoDialogConfig: function() 
        { 
         return new Dialogs.YesNoDialogConfig(Title, Message); 
        } 
       } 

      }).result; 

而且它的工作原理:)

+0

如果它適合你,你應該將你的答案標記爲可接受的答案。 – ringstaff

+0

是的,它不會讓我說,我明天只能標記它。 – user1435899