2017-01-19 31 views
0

jsp页面:如何从日期选择器数据绑定到一个普通NG-模型

<label>Script ID:</label> 
     <input name="scriptId" type="text" 
      ng-model="selectedScript.scriptId" 
      ng-disabled="true" 
      value="{{selectedScript.scriptId}}" 
      /> 


<form> 
     <h1>Date Pannel</h1> 
     <fieldset> 
     Pick a start date:<br> 
     <input ng-disabled = "mode =='BROWSE'" type="date" id="datePickerStart" ng-model="parent.startDate" onchange="document.getElementById('datePickerEnd').setAttribute('min', document.getElementById('datePickerStart').value)"><br><br> 
     Pick an end date:<br> 
     <input ng-disabled = "mode =='BROWSE'" type="date" id="datePickerEnd" ng-model="parent.endDate" ><br><br> 
     <button ng-disabled = "mode =='BROWSE'" ng-click="setDates()" 
     >Set Dates</button> 
     </fieldset> 
    </form> 

controller.js

$scope.parent = {startDate:''}; 
$scope.parent = {endDate:''}; 
$scope.selectedScript.startDate = null; 
$scope.selectedScript.endDate = null; 


$scope.setDates = function setDates() { 
      $scope.selectedScript.startDate = $scope.parent.startDate.getTime(); 
      $scope.selectedScript.endDate = $scope.parent.endDate.getTime(); 

      myAppFactory.setDates($scope.selectedScript.startDate, $scope.selectedScript.endDate, $scope.selectedScript.src).success(function(data) { 
       $scope.selectedScript.src=data; 
      }); 

     } 

这就是我所需要的代码得到一些东西,做...

现在我有一个表,我的JSP页面上...

<tr 
       ng-repeat="s in scripts 
       ng-click="selectScript(s, $index)" 
       ng-class="getSelectedClass(s)"> 
       <td>{{s.scriptId }}</td> 
       <td>{{s.fileName }}</td> 
</tr> 

个controller.js

$scope.selectScript = function(script, index) { 
      if ($scope.mode != 'BROWSE') 
       return; 
      $scope.parent.startDate = $scope.selectedScript.startDate; <-- this part 
      $scope.parent.endDate.setTime = $scope.selectedScript.endDate; <--and this 

      $scope.selectedScript = script; 
      $scope.selectedRow = index; 
     }; 

当我保存脚本,在数据库中的数据的startDate和结束日期 的作品......

我的问题是selectedScript为模型,在上面的代码,在startDate和endDate中没有任何值...它为空(但字段存在,并且数据库有我需要的数据)

但是我需要某种方式使parent.startDate和endDate绑定成为可能到selectedScript的startDate和endDate ...

顺便说一句,我第一次尝试使日期选择器的ng模型像selectedScript.startDate,但没有工作... 后来我发现datepicker有它自己的范围,这是我的问题..至少我相信如此。

我只是希望我已经让自己清楚... 你能帮助我吗?

回答

1

我认为你的问题是因为当你从数据库中检索数据时,它将它作为字符串传递,它不是datepicker可以读取的格式。

基本上你需要做的是将它解析为日期属性,做到这一点你可以做到;

$scope.parent.startDate = new Date($scope.selectedScript.startDate); 
$scope.parent.endDate = new Date($scope.selectedScript.endDate); 

在相关的地方使用这个解析为日期,或者你可以设置一个手表为你改变值;

$scope.$watch('parent.startDate', function(startDate){ 
$scope.parent.startDate = new Date($scope.parent.startDate); 
}); 

您只能使用$ watch作为单个值,或者您可以将整个对象视为“父”。

希望它有帮助!

+0

thx男人,它帮助 – newbie

相关问题