angular schema-form modal…problems with $scope

2019-06-19 17:27发布

问题:

Plunker

This plunker allows you to edit rows in a grid. I have created a new method based on RowEditCtrl to insert a new row but am having trouble with the validation.

When I insert a new row, the form is "pristine and valid". In the insert method, I need to call $scope.$broadcast('schemaFormValidate') which will validate the form and form.$valid will be false. Ideally, I would like to call this check from ng-show on the save button so the button does not appear until the form is ok.

The problem is, I don't understand or know how to get the schema-form $scope in this RowEditCtrl method and cannot get the form to be invalid before the user has typed anything.

function RowEditCtrl($modalInstance, PersonSchema, grid, row) {
  var vm = this;
  
  vm.schema = PersonSchema;
  vm.entity = angular.copy(row.entity);
  vm.form = [
    'name',
    'company',
    'phone',
    {
      'key': 'address.city',
      'title': 'City'
    },
  ];
  
  vm.save = save;
  
  function save() {
    // Copy row values over
    row.entity = angular.extend(row.entity, vm.entity);
    $modalInstance.close(row.entity);
  }
}

回答1:

I was able to access $scope in RowEditCtrl by passing $scope into editRow's $modal.open controller. See my fork of the original the plunkr here: http://next.plnkr.co/edit/NiwF12BLJQyD6Cku

Here are the relevant changes:

function editRow(grid, row) {
    $modal.open({
      templateUrl: 'edit-modal.html',
      controller: ['$scope', 

...

function RowEditCtrl($scope, 

...

function save() {
    console.log($scope.$broadcast('schemaFormValidate'));

When I do this console.log of $scope.$broadcast('schemaFormValidate') within the modal's save function it seems to be working. Or, at least $scope is defined and $broadcast callable. Apologies if this does not address the question well enough, I am not familiar with schemaFormValidate.