2015-03-03 46 views
0

所以我有這個plnkr。我試圖找出如何讓用戶能夠提交表單,儘管有一個空的「項目組」即用戶可以提交儘管同時有item.fooitem.bar空,但不是當一個表單控件isn' t空了。

回答

2

改變你的模板是:

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>', 
    } 
    }) 

通知NG要求的屬性。

這基本上說,當bar被評估爲真時,需要make foo。並且僅當foo被評估爲真時才使欄需要

看到它住在這裏:http://plnkr.co/edit/pCq8MfgLkZALaWcRLLNz?p=preview