2015-10-19 182 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

相关问题