2015-10-19 180 views
1

嗨我試圖從頁面傳遞參數到指令控制器。我可以將它傳遞給指令,但是如何在指令控制器的範圍內使用它。 我的指令:如何將參數傳遞給指令控制器?

var modalViewUrl = function ($modal) { 
return { 
    restrict: 'A', // A: attribute 
    scope: { // isolate scope 
     'modalViewUrl': '@', // modal view url to render the modal content 
     'modalController': '@', // modal view controller (optional) 
     'modalSize': '@', // modal view size (optional) sm, md, lg 
     'widgetId': '@' 
    }, 
    link: function($scope, element, attrs){ 

     element.bind('click', function(){ 

      var template = '<div ng-include="\'' + 'Dashboards/' + $scope.modalViewUrl + '\'"></div>'; 

      var modalInstance = $modal.open({ 
       animation: true, 
       template: template, 
       size : $scope.modalSize, 
       controller: $scope.modalController,      
      }); 
     }); 
    } 
}; 
} 

和這個網站:

<a href class="config" modal-view-url="WidgetSettingsModal" modal-controller="widgetSettingsController" widget-id="{{widget.id}}" modal-size="md"> 

其中widget.id是我想傳遞給 「widgetSettingsController」 參數。

widgetSettingsController:

var widgetSettingsController = function ($scope, $rootScope, $modalInstance, DataService) { 

// Here want to use parameter widgetId 

}; 

widgetSettingsController.$inject = ['$scope', '$rootScope', '$modalInstance', 'DataService']; 

回答

1

如同示例引導的(https://angular-ui.github.io/bootstrap/):

var modalInstance = $modal.open({ 
     animation: true, 
     template: template, 
     size : $scope.modalSize, 
     controller: $scope.modalController, 
     resolve : { 
      myid : function() { 
       return $scope.widgetId; 
      } 
     }      
    }); 

控制器現在可以使用它:

var widgetSettingsController = function ($scope, $rootScope, $modalInstance, DataService, myid) { 


widgetSettingsController.$inject = ['$scope', '$rootScope', '$modalInstance', 'DataService', 'myid']; 
+0

完美的作品,非常感謝你 – Whistler

相關問題