我有媒体列表,当我点击一个项目我要打开ngDialog并将模型传递给ngDialog时,我已经阅读了文档和博客但是所有这些都使用$ scope来传递模型或数据,而不是控制器(vm) 。如何使用vm将数据传递给ngDialog控制器以及如何从vm(Controller as)中的ngDiloag控制器调用父vm($ parent。$ scope)
这是我的代码(简化版)
(function () {
'use strict';
angular
.module('app.media')
.controller('mediaController', Controller);
Controller.$inject = ['$filter', 'ngTableParams', '$rootScope', '$http', '$log', '$uibModal', 'ngDialog', 'toaster', 'mediaDataService'];
function Controller($filter, ngTableParams, $rootScope, $http, $log, $uibModal, ngDialog, toaster, mediaDataService) {
var vm = this;
vm.media = {};
activate();
function activate() {
vm.updateMedia = function () {
mediaDataService.updateMedia(vm.media).then(function (res) {
toaster.pop('success', 'ویرایش فایل با موفقیت انجام شد', 'ویرایش فایل')
})
}
vm.openUpdateDialog = function (media) {
//i want to use vm.media in opening dialog
vm.media = media;
ngDialog.open({
template: 'media/edit'
, className: 'ngdialog-theme-default'
, controller: 'updateMediaController',
//i had used data to pass data to new controller and used ngDialogData in my opening template to access media ,
//the probelm with this case is i cant access parrent controller(the controller that is openin dialog) from DngDialog
//opened Controller(because i want to run parrent controller updateMedia function when user click update in opened dialog)
//**commented out - not usefull **//
//data:media
})
}
}
}
})();
有什么建议吗? 谢谢
答案 0 :(得分:1)
试试这个:
如果您使用具有单独$ scope服务的控制器,则此对象将传递给$ scope。$ parent param: see this for more information
ngDialog.open({
template: 'media/edit',
className: 'ngdialog-theme-default',
controller: 'updateMediaController',
scope:$scope
})