AngularJS - 将可选参数传递给模态

时间:2014-07-30 21:07:33

标签: javascript angularjs

如何将OPTIONAL参数传递给angularJS模态?这是我的代码:

CONTROLLER A(TRIGGER):

$modal.open({
  templateUrl: 'UploadPartial.html',
  controller: 'photos.uploadCtrl',
  resolve: {
    preselectedAlbum: function preselectedAlbum() {
      return angular.copy($scope.selectedAlbum);
    }
  }
});

控制器B(模式):

app.controller('photos.uploadCtrl', [
  '$scope',
  '$modalInstance',
  '$injector',
  function uploadCtrl($scope, $modalInstance, $injector) {
    if ($injector.has('preselectedAlbum')) {
      console.log('happy');  // I want this to work, but $injector doesn't find it
    } else {
      console.log('sad');  //  Always gets here instead :(
    }
  }
]);

注意:当我将preselectedAlbum作为依赖项时,它会起作用,但只要我没有明确地传入它,我就会收到错误。我希望它是可选的。

2 个答案:

答案 0 :(得分:6)

将值附加到模态控制器

angular.module('app')
    .controller('TestCtrl',TestCtrl)
    .value('noteId', null); // optional param

答案 1 :(得分:3)

除了resolve:之外,您还可以通过scope:将值传递给模态控制器。

$scope.preselectedAlbum = angular.copy($scope.selectedAlbum);

$modal.open({
  templateUrl: 'UploadPartial.html',
  controller: 'photos.uploadCtrl',
  scope: $scope,
});

然后在模态控制器中:

function uploadCtrl($scope, $modalInstance) {
  if ($scope.preselectedAlbum) {
    console.log('happy');
  } else {
    console.log('sad');
  }
}

示例plunker: http://plnkr.co/edit/ewbZa3I6xcrRWncPvDIi?p=preview