如何将从html元素传递的对象传递到父控制器中的函数到父控制器内的控制器

时间:2017-12-09 08:42:46

标签: angularjs meanjs

我试图将从html元素传递的地址对象传递给父控制器(myAppController)中的函数到父控制器内的控制器(即DialogController)。我正在创建一个md对话框,当单击编辑按钮时,地址对象中的元素应显示在md对话框中。 md对话框正在运行,但无法显示地址中的项目。 这是我的控制器

(function () {
'use strict';
angular
.module('myApp')
.controller('myAppController', myAppController);
myAppController.$inject = ['$scope', '$state','AddressesService'];
function myAppController ($scope, $state, AddressesService) {
var vm = this;
vm.addresses = AddressesService.query();
// this is a long controller so i cut short it to important part
$scope.showAdvanced = function(address, ev) {
$mdDialog.show({
    controller: DialogController(address),
    templateUrl: '/modules/carts/client/views/edit.tmpl.html',
    parent: angular.element(document.body),
    targetEvent: ev,
    clickOutsideToClose:true,
    fullscreen: $scope.customFullscreen // Only for -xs, -sm breakpoints.
  });
function DialogController($scope, $mdDialog, address) {
console.log("address"+" "+address); // says undefined
    $scope.hide = function() {
      $mdDialog.hide();
    };
$scope.cancel = function() {
      $mdDialog.cancel();
    };
    $scope.answer = function(answer) {
      $mdDialog.hide(answer);
    };
  }
 };
}
}());`

这是我的HTML

<div ng-repeat="address in vm.addresses">
<h5 class="list-group-item-heading uppercase bold" ng-bind="address.name"</h5>
<button ng-click="showAdvanced(address, $event)">Edit</button>
</div>`

1 个答案:

答案 0 :(得分:0)

将数据传递给对话框控制器的正确方法是使用参数'locals'

试试这个:

$mdDialog.show({
controller: 'DialogController',
templateUrl: '/modules/carts/client/views/edit.tmpl.html',
parent: angular.element(document.body),
targetEvent: ev,
clickOutsideToClose:true,
fullscreen: $scope.customFullscreen,
locals: {
  address: address
  }
});

请注意,您的控制器名称应在引号之间:''