Javascript ng指令和$validator的重复

Javascript ng指令和$validator的重复,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,所以我有这个。我正在试图找出如何允许用户在“项目组”为空的情况下提交表单,即用户在item.foo和item.bar均为空的情况下提交表单,但在其中一个表单控件不为空的情况下不提交表单。将模板更改为: angular.module('plunker', []).controller('MainCtrl', function($scope) { $scope.myModel = {}; $scope.myModel.items = []; $scope.myMode

所以我有这个。我正在试图找出如何允许用户在“项目组”为空的情况下提交表单,即用户在
item.foo
item.bar
均为空的情况下提交表单,但在其中一个表单控件不为空的情况下不提交表单。

将模板更改为:

    angular.module('plunker', []).controller('MainCtrl', function($scope) {
    $scope.myModel = {};
    $scope.myModel.items = [];
    $scope.myModel.items.push({ foo: 'foo', bar: 'bar' });
  }).directive('myDirective', function() {
    return {
      require: 'ngModel',
      scope: {
        myModel: '=ngModel',
      },
      link: function(scope, elem, attrs, modelCtrl) {

      },
      template: '<ng-form name="add">' + 
        '<input type="text" name="foo" ng-required="myModel.bar" ng-model="myModel.foo" />' +
        '<input type="text" name="bar" ng-required="myModel.foo" ng-model="myModel.bar" />' +
        '</ng-form>',
    }
  })
angular.module('plunker',[]).controller('MainCtrl',function($scope){
$scope.myModel={};
$scope.myModel.items=[];
$scope.myModel.items.push({foo:foo',bar:bar'});
}).directive('myDirective',function(){
返回{
要求:'ngModel',
范围:{
myModel:'=ngModel',
},
链接:函数(范围、元素、属性、模型控制){
},
模板:“”
'' +
'' +
'',
}
})
请注意ng required属性

这基本上是说,当bar被计算为true时,makefoo是必需的。并且仅当foo的计算结果为true时才需要使用bar

现场观看: