2015-07-02 91 views
1

我有一個表單,我試圖將日期選取器對象轉換爲指令。我能夠將數據輸入到輸入中,但它不會將數據與傳遞給它的範圍變量綁定。這是我的代碼。Angular Custom Directive數據綁定

查看:

<ng-date-picker id-attr="lastRedemptionDate" ng-model="spr.lastRedemptionDate"></ng-date-picker> 

指令:

myApp.directive('ngDatePicker', function() { 
    return { 
     restrict: 'AEC', 
     templateUrl: 'assets/angular/directives/datePicker.html', 
     replace: true, 
     scope: { 
      objID: '@idAttr', 
      personName: "=ngModel" 
     }, 
     link: function($scope, elem, attrs, controllerInstance) { 
      $scope.$apply($scope.method()); 

      $('.datepicker').datepicker({ 
       format: 'm/d/yyyy', 
       autoclose: true 
      }); 

      console.log($scope); 
     } 
    } 
}) 

指令模板:

<div class='input-group input-append date'> 
    <input type='text' class="form-control datepicker" id="{{ objID }}" value="{{ personName }}" /> 
    <span class="input-group-addon"> 
     <span class="glyphicon glyphicon-calendar"></span> 
    </span> 
</div> 

任何幫助將不勝感激。

回答

0

使用ngModel指令綁定模型的輸入,而不是值屬性:

<div class='input-group input-append date'> 
    <input type='text' class="form-control datepicker" ng-model="personName" id="{{ objID }}" /> 
    <span class="input-group-addon"> 
     <span class="glyphicon glyphicon-calendar"></span> 
    </span> 
</div> 
+0

完美,非常感謝你! – wfsdesign