2015-01-21 566 views
7

我用AngularJs和jQuery寫了一些代碼。只有一件事可行,無論是AngularJs還是jQuery。AngularJS和jQuery衝突

看來,jQuery和AngularJs相互衝突。只要我包含這兩個庫,它們就不能正常工作了。如果我只包含其中的一個(AngularJS或Jquery),那麼這部分工作。

但我不明白問題出在哪裏:...

$(document).ready(function() { 

     $('#Text').focus(); 
      $(document).on("mouseover", ".englishMtg", function() { 
      var currentHref = $(this).attr("href"); 
      if (currentHref.indexOf("http") == -1) { 
       var changedLink = currentHref.match(/[^\/?#]+(?=$|[?#])/); 
       var englishSearchString = ".../query?q="+changedLink.toString().replace('.jpg', '').split(',')[0]; 
       $(this).attr("href", englishSearchString); 
      } 


     }); 

     $(document).on("mouseover", ".germanMtg", function() { 
      var currentHref = $(this).attr("href"); 
      if (currentHref.indexOf("http") == -1) { 
       var changedLink = currentHref.match(/[^\/?#]+(?=$|[?#])/); 
       var germanSearchString = "http://..../query?q="+changedLink.toString().replace('.jpg', '').split(',')[1]; 
       $(this).attr("href", germanSearchString); 
      } 
     }); 

     $(document).on("mouseover", ".tumbailImage", function() { 
      var currentHref = $(this).attr("src"); 
      var changedLink = currentHref.match(/[^\/?#]+(?=$|[?#])/); 

      $(this).closest(".thumbnail").attr("title", changedLink.toString().replace('.jpg', '').split(',')[1]); 

      //alert(changedLink.toString().replace('.jpg', '').split(',')[1]); 
     }); 

    }); 

AngularJS(代碼)部分:

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

function MyCtrl($scope) { 

    $scope.currentPage = 0; 
    $scope.pageSize = 18; 
    $scope.data = ['...']; //some data 

    $scope.numberOfPages=function(){ 
     return Math.ceil($scope.filtered.length/$scope.pageSize); 
    } 

} 


app.filter('startFrom', function() { 
    return function(input, start) { 
     start = +start; //parse to int 
     return input.slice(start); 
    } 
}); 

AngularJS(HTML-部分):

<div class="form-horizontal" ng-app="myApp" ng-controller="MyCtrl" ng-init="imageHeight=300;imageWidth=400"> 
<div class="form-group"> 
    <div class="col-md-12"> 
     <div class="panel-group col-md-12" id="accordion"> 
      <div class="panel panel-default"> 
       <div class="panel-heading"> 
        <div class="panel-title"> 
         <div style="text-align:center"> 
          <button class="btn btn-default" ng-disabled="currentPage == 0" ng-click="currentPage=currentPage-1"> 
           Previous 
          </button> 
          <a class="label label-default" style="font-size:large" data-toggle="collapse" data-parent="#accordion" href="#collapseOne"><span class="badge"> {{currentpage+1}}/{{numberOfPages()}} ({{filtered.length}} results)</span></a> 
          <button class="btn btn-default" ng-disabled="currentPage >= filtered.length/pageSize - 1" ng-click="currentPage=currentPage+1"> 
           Next 
          </button> 
         </div> 
        </div> 
       </div> 
       <div id="collapseOne" class="panel-collapse collapse in"> 
        <div class="panel-body"> 
         <div class="input-group col-md-4"> 
          <span class="input-group-addon editor-label">search</span> 
          <input type="text" class="form-control" placeholder="search" ng-model="searchImage"> 
         </div> 
         <div class="input-group col-md-4"> 
          <span class="input-group-addon editor-label">width</span> 
          <input type="text" class="form-control" placeholder="width" ng-model="imageWidth"> 
          <span class="input-group-addon">px</span> 
         </div> 
         <div class="input-group col-md-4"> 
          <span class="input-group-addon editor-label">height</span> 
          <input type="text" class="form-control" placeholder="height" ng-model="imageHeight"> 
          <span class="input-group-addon">px</span> 
         </div> 
         <br /> 
         <div class="clearfix"></div> 
         <div class="col-md-4 " ng-repeat="item in filtered = (data | filter: searchImage) | startFrom:currentPage*pageSize | limitTo:pageSize" style="overflow:auto"> 
          <a href="{{item}}" title="{{item}}" target="_blank" class="germanMtg"><img src="~/Pictures/german.png" /></a> 
          <a href="{{item}}" title="{{item}}" target="_blank" class="englishMtg"><img src="~/Pictures/uk.png" /></a> 
          <a href="{{item}}" title="{{item}}" target="_blank" class="thumbnail"><img ng-src="{{item}}" class="tumbailImage" height="{{imageHeight}}" width="{{imageWidth}}"></a> 

         </div> 
        </div> 
       </div> 
      </div> 
     </div> 
    </div> 
</div> 

我得到這個錯誤,即使我f我使用jQuery 1.6(僅有時):

Error: $scope.filtered is undefined MyCtrl/[email protected]://localhost:5891/Scripts/Angular/JSONMtgs/jsonAngularJs.js:26200:9 _functionCall/<@http://localhost:5891/Scripts/Angular/Source/angular_1_0_3.js:6193:13 $interpolate/[email protected]://localhost:5891/Scripts/Angular/Source/angular_1_0_3.js:4810:22 $RootScopeProvider/this.$get</[email protected]://localhost:5891/Scripts/Angular/Source/angular_1_0_3.js:7720:32 $RootScopeProvider/this.$get</[email protected]://localhost:5891/Scripts/Angular/Source/angular_1_0_3.js:7926:13 bootstrap/<@http://localhost:5891/Scripts/Angular/Source/angular_1_0_3.js:930:7 [email protected]://localhost:5891/Scripts/Angular/Source/angular_1_0_3.js:2802:1 [email protected]://localhost:5891/Scripts/Angular/Source/angular_1_0_3.js:928:1 [email protected]://localhost:5891/Scripts/Angular/Source/angular_1_0_3.js:904:5 @http://localhost:5891/Scripts/Angular/Source/angular_1_0_3.js:14527:5 jQuery.Callbacks/[email protected]://localhost:5891/Scripts/Jquery/jquery-1.10.2.js:3048:10 jQuery.Callbacks/[email protected]://localhost:5891/Scripts/Jquery/jquery-1.10.2.js:3160:7 [email protected]://localhost:5891/Scripts/Jquery/jquery-1.10.2.js:433:1 [email protected]://localhost:5891/Scripts/Jquery/jquery-1.10.2.js:104:4 


return logFn.apply(console, args); 

angular_1_0_3.js (Zeile 5582) 

謝謝你的幫助。

+0

您需要在加載AngularJS之前加載JQuery。 – 2015-01-21 10:27:39

+0

我知道,但比AngularJS不再真的工作(只有jQuery的一部分)... – user254197 2015-01-21 10:29:02

+0

發表您的其他代碼:完整的html頁面... – Nathan 2015-01-21 10:29:09

回答

0

嘗試使用firebug來捕獲JS/JQuery/AJS的錯誤。 firebug是Mozilla Firefox的插件,你應該已經安裝了Firefox的更新版本。

+0

更新我的ansewer – user254197 2015-01-23 10:56:10

1

是的,如果應用程序正在引導時,Angular可以使用jQuery。如果腳本路徑中沒有jQuery,則Angular會回退到我們稱之爲jQLite的jQuery子集的自身實現。 Angular 1.3只支持jQuery 2.1或更高版本。 jQuery 1.7和更新版本可能會與Angular正常工作,但我們不能保證。

我也建議你只使用JQLite ..它具有幾乎Jquery中的功能。但是,如果您想使用Jquery而不是Lite版本,請在Angular-Script之前加載Jquery。

10

jQuery和AngularJS都應該以透明的方式一起工作。如果jQuery在AngularJS之前加載,AngularJS實際上將使用可用的jQuery而不是「迷你」內置jQuery克隆(jqLit​​e)。

然而,在常見問題解答(https://docs.angularjs.org/misc/faq)中提到「Angular 1.3只支持jQuery 2.1或更高版本,jQuery 1.7和更新版本可能與Angular正常工作,但我們不保證。」。根據你的Angular和jQuery版本,這可能是你問題的原因。