如何将参数传递给指令控制器?

时间:2015-10-19 13:42:48

标签: angularjs

您好我试图将参数从页面传递到指令控制器。我可以将它传递给指令,但是如何在指令控制器的范围内使用它。 我的指示:

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,                    
            });
        });
    }
};
}

和这个html:

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

其中widget.id是我想传递给&#34; widgetSettingsController&#34;的参数。

widgetSettingsController:

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

// Here want to use parameter widgetId   

};

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

1 个答案:

答案 0 :(得分:1)

就像bootstrap(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'];