2017-07-31 183 views
0

我正在使用'ui.bootstrap.contextMenu'來構建上下文菜單。當我右鍵點擊一個字段時,我需要傳入一個唯一的ID給控制器,並返回一個基於ID的選項列表。每個列表可能不同。如果我使用硬編碼列表,它工作正常,但我似乎無法動態生成列表。我錯過了什麼?示例代碼:使用ui.bootstrap.contextMenu動態上下文菜單

`<div> 
    <textarea ng-model="ctrl.status" context-menu="ctrl.menuOptions (ctrl.id)"></div> 
</div> 
vm.menuOptions = function(id) = { 
    var listArray = $scope.list; // array of possible list items based on ID 
    angular.forEach(listArray, function(value, key) { 
    if (id === value.id) { 
     return [ 
      [value.id, function ($itemScope) { 
       return value.textName; 
      }],     
     ] 
    } 
    }  
}` 

回答

0

我假設你指的是這個實現:https://github.com/Templarian/ui.bootstrap.contextMenu

的函數生成上下文菜單傳遞三件事情:

  • $ cmScope - 範圍的上下文菜單
  • 事件 - 觸發上下文菜單打開的事件對象
  • modelValue - n的值附加到當前對象

所以,在你的榜樣G-模型,你可以使用:

vm.menuOptions = function($cmScope, event, modelValue) = { 
    var listArray = $scope.list; // array of possible list items based on ID 
    //^Assuming that the $scope here is the scope of the container, not the contextmenu 
    angular.forEach(listArray, function(value, key) { 
    if (modelValue === value.id) { 
     return [ 
     [value.id, function ($itemScope) { 
      return value.textName; 
     }],     
     ] 
    } 
    }  
}