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

Jer*_*itz 3 javascript angularjs

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

控制器A(TRIGGER):

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

控制器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 :(
    }
  }
]);
Run Code Online (Sandbox Code Playgroud)

注意:当我把它preselectedAlbum作为一个依赖项时它可以工作,但是每当我没有明确地传入它时我就会得到错误.我希望它是可选的.

Ste*_*eve 6

将值附加到模态控制器

angular.module('app')
    .controller('TestCtrl',TestCtrl)
    .value('noteId', null); // optional param
Run Code Online (Sandbox Code Playgroud)


run*_*arm 5

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

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

$modal.open({
  templateUrl: 'UploadPartial.html',
  controller: 'photos.uploadCtrl',
  scope: $scope,
});
Run Code Online (Sandbox Code Playgroud)

然后在模态控制器中:

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

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