AngularJS和JQuery玩得不好,ng-show无法正常工作

时间:2016-12-08 00:28:12

标签: jquery angularjs

我有一个带有jquery日历的页面,然后是一些表单,用于提交一些事件。一切都很好,直到我决定使用angularjs进行表单验证。

问题是我的错误消息p元素永远不会丢失显示:无。

如果我删除jquery脚本声明它可以工作,但日历已经消失。

我在底部有jquery的脚本,所以我把它移到顶部但没有区别。

似乎应该有一个简单的解决方法,但如果我能弄清楚的话,我会的。

这是表单元素之一。

<div>
  <input type="text" name="authorname" placeholder="Author Name" ng-model="book.authorname" ng-required="true">
  <p class="error" ng-show="authorname.$invalid && authorname.$touched">
    You must fill out your author name.</p>
</div>

1 个答案:

答案 0 :(得分:0)

角度验证仅适用于表单。如果您想在表单之外使用验证,则必须将ng-form声明为顶部容器。

(function(angular) {
  'use strict';
  angular.module('Example', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.test = function(){}
    }]);
})(window.angular);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="Example">
    <div ng-controller="ExampleController" >
      <form name="bookForm">
        <input type="email" name="authorname" ng-model="book.authorname" ng-required="true" />
        <input type="text" ng-click="test()" placeholder="Test" />
        <p class="error" ng-show="bookForm.authorname.$invalid">
    You must fill out your author name.</p>
       </form>
    </div>
  </body>