javascript - 在 Angular-ui-modal 中使用 Angular-ui-bootstrap 日期的奇怪行为

标签 javascript html angularjs angular-ui-bootstrap

我正在使用 Angular 开发我的第一个应用程序,但我看不出有什么问题。

我在其他地方使用了 ui-bootstrap date ,没有出现任何问题,但后来我尝试在模态中使用它,每当您第一次在模态中选择日期时,它都会正常工作,但如果您选择了错误的日期并且想要选择正确的日历,单击按钮再次打开日历,如果它位于模态框内,则不起作用。

我创建了一个plunker sample where the error is reproduced .

代码片段如下:

index.html

<!DOCTYPE html>
<html ng-app="myApp">

<head>
<link data-require="bootstrap-css@3.1.1" data-semver="3.1.1" rel="stylesheet"      href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
<link data-require="bootstrap-theme@3.2.0" data-semver="3.2.0" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css" />
<script data-require="angular.js@1.3.0-beta.5" data-semver="1.2.21" src="https://code.angularjs.org/1.2.21/angular.js"></script>
<script data-require="angular-ui-bootstrap@0.11.0" data-semver="0.11.0" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.min.js"></script>
<script src="app.js"></script>
</head>

<body>
<h1>Angular ui date!</h1>
<div ng-controller="DateController">
  <div class="row">
      <div class="col-md-6">
          <h4>Date(This works)</h4>
          <p class="input-group">
              <input type="text" class="form-control" datepicker-popup="{{format}}" disabled
                     ng-model="activityDate" is-open="opened" datepicker-options="dateOptions"
                     ng-required="true" close-text="Close"/>

            <span class="input-group-btn">
              <button type="button" class="btn btn-default" ng-click="open($event)"><i
                      class="glyphicon glyphicon-calendar"></i></button>
            </span>
          </p>
      </div>
  </div>
  <div class="row">
      <div class="col-md-6">
          <button type="button" class="btn btn-primary" ng-click="openModal()">Open Date Modal</button>
      </div>
  </div>
</div>

dateModal.html

<div>
<div class="modal-header">
  <h3 class="modal-title">Date Modal Sample</h3>
</div>
<div class="modal-body">
<div class="row">
  <div class="col-md-6">
      <h4>Modal Date(works only the first time! whyyyyyy?????)</h4>

      <p class="input-group">
          <input type="text" class="form-control" datepicker-popup="{{format}}" disabled
                 ng-model="dateModal" is-open="opened"
                 ng-required="true" close-text="Close"/>
            <span class="input-group-btn">
              <button type="button" class="btn btn-default" ng-click="open($event)">
                <i class="glyphicon glyphicon-calendar"></i>
              </button>
            </span>
      </p>
  </div>
</div>
</div>
<div class="modal-footer">
  <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>
</div>

app.js

'use strict';

(function () {
var myApp = angular.module('myApp', ['ui.bootstrap']);

myApp.controller('DateController', ['$scope', '$modal', '$log', function($scope, $modal, $log){
  //Initializes date
    $scope.today = function () {
        $scope.activityDate = new Date();
    };

    //open calendar to select initial date
    $scope.open = function ($event) {
        $event.preventDefault();
        $event.stopPropagation();
        $scope.opened = true;
        $log.info('open function was called');
    };

    $scope.today();
    $scope.format = 'MM/dd/yyyy';
    
    
    //open modal window
    $scope.openModal = function () {
        var modalInstance = $modal.open({
            templateUrl: 'dateModal.html',
            controller: 'DateModalController'
        });
    }
  
}]);

myApp.controller('DateModalController', ['$scope', '$modalInstance', function($scope, $modalInstance){
  
  $scope.cancel = function () {
      $modalInstance.dismiss('Cancel');
  };
  
  //Initializes date
  $scope.today = function () {
      $scope.dateModal = new Date();
  };
  
  //open calendar to select initial date
  $scope.open = function ($event) {
      $event.preventDefault();
      $event.stopPropagation();
      $scope.opened = true;
      $log.info('open button from modal calendar field has been pressed!!!!');
  };
  $scope.today();
  $scope.format = 'MM/dd/yyyy';
}]);

})();

最佳答案

作用域继承自其父作用域。

该模式是作为您在初始化时传入的范围的子级创建的(默认情况下是在 $rootScope 上)。当尝试直接在范围上设置属性时,Angular 会自动为您创建它。但是,如果您尝试执行 model.myProperty,如果子作用域中不存在 model,它将向上移动到父作用域并在那里正确设置它(如果它存在)。存在)。

关于作用域如何工作的详细描述可以在这里找到:https://github.com/angular/angular.js/wiki/Understanding-Scopes

这是一个工作示例,无需使用 $parent

http://plnkr.co/edit/WeJqirLDOoFuTqJEHEdg

 <input type="text" class="form-control" datepicker-popup="{{format}}" disabled
                     ng-model="dateModal.date" is-open="dateModal.opened" 
                     ng-required="true" close-text="Close"/>

关于javascript - 在 Angular-ui-modal 中使用 Angular-ui-bootstrap 日期的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25395166/

相关文章:

javascript - div 上的基本 ng-show 不起作用

javascript - $(function() vs jQuery(document).ready

javascript - 显示隐藏 div 不起作用

javascript - 如何根据数组中的数据动态创建表

html - HTML 电子邮件中的图像 mask

javascript - 如何在 ng-repeat 中使用指令?

angularjs - ng-model 和 ng-bind 有什么区别

javascript - 无法读取未定义的属性 'toLowerCase' - jQuery

javascript - 下拉焦点位于页面上并使用 jquery/javascript 输入按键检查

html - 切换点击不响应