2014-09-30 62 views
-2

我有非常簡單的Jquery代碼,它從不顯示第二個提醒。有人知道什麼是錯的嗎?從未調用JQuery更改函數

 <script type="text/javascript"> 
      $(document).ready(function() { 
       alert("1"); 
       $(".images_clicable input").change(function(){ 
         alert("2"); 
       }); 
      }); 
      $(".images_clicable input").change(function(){ 
       alert("2"); 
      }); 
     </script> 
     <div> 
      <div class="images_clicable addPhoto">      
       <label for="label_image1"> 
        <img class="img_preview" id="image1" src="./img/plus.png" height="100px"> 
       </label> 
       <input name="image1" id="label_image1" type="file">      
      </div> 
     </div> 

此代碼在角度視圖內。

+0

我在裏面和外面都添加了它。 – 2014-09-30 20:45:53

+2

這與AngularJS有什麼關係? – Darren 2014-09-30 20:46:12

+0

這段代碼是角度改變的內部視圖。我不是角度大師,也不知道它是否重要。 – 2014-09-30 20:47:23

回答

0

,你有問題的原因是,你不處事的方式AngularJS,但在jQuery的方式做他們。您想要在Angular中完成的任務應該使用directives來代替。從指令中,您可以訪問jQuery的jqLit​​e接口,該接口將包含90%的您可能想要對jQuery執行的任何操作。

下面是一個例子指令:

app.directive('alertOnChange', function() { 
    return function(scope, element, attrs) { 
    element.on('change', function() { 
     alert("It works!"); 
    }); 
    }; 
}); 

下面是修改<input>標籤:

<input alert-on-change name="image1" id="label_image1" type="file"> 

Here是plunker。

+0

我已經寫了JavaScript的代碼,但仍然不明白爲什麼它不起作用。Angular這也是JS,不是嗎?那麼爲什麼不可能調用JS代碼從角? plnkr.co/edit/46keW5QzHNnSJBTrjhXi – 2014-09-30 21:51:58

+0

以下是文件上傳的示例:https://egghead.io/lessons/angularjs-file-uploads。此外,這可能會有所幫助,如果你還沒有看到它:http://stackoverflow.com/questions/14994391/how-do-i-think-in-angularjs-if-i-have-a-jquery-background ?LQ = 1 – Bobby 2014-09-30 22:01:52

0

這段代碼適用於我,我不熟悉角度重寫的角度。

function readURL(input) { 
    if (input.files && input.files[0]) { 
     var reader = new FileReader(); 
     //check the name of input 

     var input_name = $(input).attr('name'); 
     reader.onload = function (e) { 
      $('#'+input_name).attr('src', e.target.result); 
      $('#label_'+input_name).attr('value', e.target.result); 
     } 
     reader.readAsDataURL(input.files[0]); 
    } 
} 

    $(".images_clicable input").change(function(){ 
     readURL(this); 
    });