1

我想用 jquery 插件 timepicker 制作一个自定义指令。我没有在控制台中获得输入值,它说未定义。

这是一个plunkr

<table class="table table-bordered">
  <thead>
    <tr>
      <th>Time From</th>
      <th>Time To</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <input type="text" ng-model="row1" size=6/ disabled>
      </td>
      <td>
        <input type="text"   ng-model="dup_row1 " size=6 timepicki/>
        {{dup_row1}}
      </td>
    </tr>

  </tbody>
</table>


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

  function() {
    var link;
    link = function(scope, element, attr, ngModel) {
      element.timepicki();
    };

    return {
      restrict: 'A',
      link: link,
      require: 'ngModel'
    };
  }
])
app.controller('ctrl', function($scope) {
  $scope.row1 = "00:00"

  $scope.submit=function(){
    console.log($scope.dup_row1)
  }
});
4

1 回答 1

0

您发布的代码与您的 plunker 中的代码不同。

AngularJS 开发人员指南说;

使用控制器:

  • 设置$scope对象的初始状态。

在上面的示例中,您记录了$scope.dup_row1on submit 的值,但您的控制器从不设置该值,因此它是未定义的。

以下将打印“hello”到控制台;

app.controller('ctrl', function($scope) {
  $scope.row1 = "00:00"
  $scope.dup_row1 = "hello"

  $scope.submit=function(){
    console.log($scope.dup_row1)
  }
});
于 2015-08-25T15:13:42.273 回答