【问题标题】:how to pass object that is passed from html element to a function in parent controller to a controller that is inside the parent controller如何将从html元素传递给父控制器中的函数的对象传递给父控制器内部的控制器
【发布时间】:2018-03-05 17:00:22
【问题描述】:

我正在尝试将从 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>`

【问题讨论】:

    标签: angularjs meanjs


    【解决方案1】:

    将数据传递给对话框控制器的正确方法是使用参数'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
      }
    });
    

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

    【讨论】:

      猜你喜欢
      • 2011-09-30
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 2012-12-15
      • 2017-01-11
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多