2017-01-15 27 views
1

角js新手 - 我试图添加jQuery日期选择器ng模型文本框。下面是jQuery的日期选择器的链接:角ng模型不绑定jQuery日期选择器值

src="https://resources.ibe4all.com/BrokerAssist/js/BuroRaDer_DateRangePicker.js" type="text/javascript"></script><script type="text/javascript" src="https://resources.ibe4all.com/CommonIBE/js/jquery-1.4.1.js"></script<script type="text/javascript" src="https://resources.ibe4all.com/CommonIBE/js/jquery-ui-1.8.11.custom.min.js"> 

<link href="https://resources.ibe4all.com/BrokerAssist/cssnew/BuroRaDer.DateRangePicker.css" rel="stylesheet" /> 

jQuery的日期选择器代码:

<script> 
$(document).ready(function() { 
    $('#RDate').datepicker({ 
     dateFormat: 'dd/mm/yy', 
     changeMonth: true, 
     changeYear: true 
    }); 

    //$("#RDate").click(function() { 
    // $(this).focus(); 
    //}); 
}); 

function AvoidSpace(event) { 
     var k = event ? event.which : window.event.keyCode; 

     if (k == 32) return false; 

    } 
</script> 

这是我的HTML标记:

<body style="background-image: url(/Content/images/img_bg_2.jpg);" 
     ng-app="nmodule" ng-controller="ncontroller"> 
    <form name="userForm" novalidate="novalidate"> 
     <ng-form name="userForm" ng-submit="submitForm(userForm.$valid)"> 
      <input type="text" name="RDate" ng-model="RDate" id="RDate" 
        placeholder="Enter Registration Date" required /> 
      {{RDate}} 
     </ng-form> 
    </form> 
</body> 

日期选取器工作就像一个魅力,但是,我面临的问题是日期不与我的角度模型绑定,我无法获得日期在$范围加我的验证不工作的文本框,因为它不考虑日期作为通过日期的输入选择器。

请帮我工作plunker

回答

1

这是百达做jQuery的投入和角模型直开箱这个的问题,意味着你经常需要手动做一些事来更新模型,或者告诉角输入事件已被触发。

更新模式
我你的情况,你可以你的位指示内更新模型,喜欢的东西:

app.controller('MainCtrl', function($scope) { 

$('#RDate').datepicker({ 
    dateFormat: 'dd/mm/yy', 
    changeMonth: true, 
    changeYear: true, 
    onSelect: function(date) { 
    $scope.RDate = date; 
    $scope.$apply(); 
    console.log($scope); 
    } 
}); 

});

因此,请选择您设置模型值和应用范围。
我做了一个plunker说明https://plnkr.co/edit/kzdUPDcVDNGRoM9vlA4K?p=preview

添加触发事件
你可以添加一个triggerhandler得到改变事件,并通知角是这样的:

app.controller('MainCtrl', function($scope) { 

    $('#RDate').datepicker({ 
     dateFormat: 'dd/mm/yy', 
     changeMonth: true, 
     changeYear: true, 
     onSelect: function(date) { 
      angular.element($('#RDate')).triggerHandler('input'); 
     } 
    }); 
    }); 

发了plunker看这个:https://plnkr.co/edit/VQAqZcLarDBTW2MFaOY7?p=preview