ng-focus不适用于angularjs中的输入文本?

时间:2015-08-01 14:21:21

标签: javascript jquery angularjs

我想使用 ng-focus 作为输入文字,它不起作用,但它提供旧值,第一次。我想在控制器中使用日期字段值来创建动态URL,它将获取JSON数据对应的日期,所以我在这里创建对应该日期的动态URL,所以我需要控制器中的日期。

还有一件事,我关注演示我正在使用指令,fromdatepicker和todatepicker,还有其他方法吗?

Pluker DEMO http://plnkr.co/edit/1KRNBUXyK5O6LNwDS06x?p=preview

HTML

<input ng-model="from_date" ng-focus="changeFromDate()" type="text"/>

Angularjs

app.controller('myCtrl', function($scope) {
     $scope.item = ""
    $scope.changeFromDate = function() {
        $scope.$apply();
        alert($scope.item);
    }
});

2 个答案:

答案 0 :(得分:2)

我猜这是你想要实现的目标:

&#13;
&#13;
<!doctype html> 
<html lang="en">
<head>
  <meta charset="utf-8" />  
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
  <script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>  
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script>
  <script>

    var app = angular.module('myApp', []);

    app.directive('datepicker', function() {
       return {
         link: function(scope, element, attrs, ctrl) {
           element.datepicker({
               inline: true,
               dateFormat: 'dd.mm.yy',
               onSelect: function(dateText) {
                   ctrl.$setViewValue(dateText);
               }
           });
         },
         require: 'ngModel'
       }
    });

    app.controller('myCtrl', function($scope) {
        $scope.todate = "";
        $scope.fromdate = "";
        $scope.changeDate = function(date) {
            alert(date);
        }
    });
  
  </script>
</head>
<body ng-app="myApp">
 <div ng-controller="myCtrl">

<p>From Date : <input type="text" datepicker ng-model="fromdate"  ng-change="changeDate(fromdate)"/></p>
 
<p> To Date: <input type="text" datepicker ng-model="todate"  ng-change="changeDate(todate)"/></p>
 
 <br />

 </div>
</body>
</html>
&#13;
&#13;
&#13;

http://plnkr.co/edit/D5sT4WEufLwoMHYxngmA?p=preview

基本上每次文本输入更改时都会触发ngChange(调用$ setViewValue)。

有关详细信息,请查看ngModelControllerngChange

答案 1 :(得分:0)

您应该尝试使用$ broadcast将指令中的详细信息发送到控制器。请参阅此plunkr:http://plnkr.co/edit/HTPJn57VPbmPUy0rnzms

这里有相关的帮助代码:

var app = angular.module('myApp', []);

app.directive('fromdatepicker', function() {
   return function(scope, element, attrs) {
       element.datepicker({
           inline: true,
           dateFormat: 'dd.mm.yy',
           onSelect: function(dateText) {
               var modelPath = $(this).attr('ng-model');
               putObject(modelPath, scope, dateText);
               scope.$apply();
               scope.$broadcast('dateSelected', {'date': dateText} );
           }
       });
   }
});
app.controller('myCtrl', function($scope) {
    $scope.item = ""
    $scope.$on('dateSelected', function(event, args) {
        alert(args.date);
    });
});