2017-04-07 18 views
0

我有這個函數需要一個變量,我想在我的控制器中使用它。將變量傳遞給uib控制器

我的代碼看起來像這樣

ctrl.openModal= myItem => { 
    let modal = $uibModal.open({ 
     templateUrl: 'path/to/my/template.html', 
     controller: 'modalController' 
    }); 
}; 

問題是,我不知道如何在控制器

module.controller('modalController', function ($scope) { 

    console.log(myItem); 

}); 

嘗試登錄它說,這是不確定的訪問myItem

回答

1

使用resolve。像:

resolve: { 
    myItem : function() { 
     return myItem; 
    } 
} 

然後在控制器:

module.controller('modalController', function ($scope, myItem) { 
    console.log(myItem); 
    $scope.myItem =myItem;// assign to scope variable to acces in your modal template 
}); 

//例的模態的模板:

<script type="text/ng-template" id="template.html"> 
     <div class="modal-header"> 
      <h3 class="modal-title">modal template</h3> 
     </div> 
     <div class="modal-body">  
      {{ myItem }} 
     </div> 
</script> 
0

傳遞您的數據使用範圍模式:

// Create the modal scope 
var modalScope = $rootScope.$new(); 
modalScope.item = {'foo': 'bar'}; 

var modal = $uibModal.open({ 
    templateUrl: '/partials/modal.html', 
    controller: modalController 
    scope: modalScope 
}); 

/** 
* 
* @param $scope The modal instance scope 
* @param $uibModalInstance the modal instance (injected) 
*/ 
function modalController($scope, $uibModalInstance) { 
    console.log($scope.item); 
} 
0

你可以簡單地resolve您的PARAM。就像這樣:

ctrl.openModal= myItem => { 
    let modal = $uibModal.open({ 
     templateUrl: 'path/to/my/template.html', 
     controller: 'modalController' 
     resolve: { 
      myItem:() => myItem 
     } 
    }); 
}; 

現在,你可以注入myItem控制器內:

module.controller('modalController', function ($scope, myItem) { 

    console.log(myItem); 

}); 
相關問題