[英]Calling a Javascript function from AngularJS Controller
我仍在學習將jQuery調用轉換為Angular js。 我之前已經建立了一個使用ziplookup進行zip查找的網頁
為此,我必須添加:
<script src="http://ziplookup.googlecode.com/git/public/ziplookup/zipLookup.min.js" type="text/javascript">
</script>
和
$(document).ready(function() { // When the document loads,
$.zipLookupSettings.libDirPath = 'ziplookup/' // (Optionally) set the library folder path manually
$('[name=zipcode]').blur(function(){ // Set on blur
$.zipLookup( // Do a Zip code Lookup
$(this).val(), // Zip code Field Value
function(cityName, stateName, stateShortName){ // If Successful,
$('input[name=city]').val(cityName); // Set City name
$('input[name=state_short]').val(stateName); // Set State abbreviation
$('input[name=state]').val(stateShortName); // Set State name
$('.message').html("Found Zipcode"); // Add a message
},
function(errMsg){ // If Zip couldn't be found,
$('.message').html("Error: " + errMsg); // Add an error message
});
});
});
現在我希望將其移植到angular js。 而不是在blur事件上調用zipLookup函數,而是從按鈕按下時調用它。 並設置相同的參數,但它給出: ReferenceError:zipLookupSettings未定義或ReferenceError:zipLookup未定義
$scope.zipeval = function(){
//zipLookupSettings.libDirPath = 'ziplookup/';
zipLookup( // Do a Zip code Lookup
11722, // Zip code Field Value
function(cityName, stateName, stateShortName){ // If Successful,
$log.log(cityName + stateName + stateShortName);
//$('input[name=city]').val(cityName); // Set City name
//$('input[name=state_short]').val(stateName); // Set State abbreviation
//$('input[name=state]').val(stateShortName); // Set State name
//$('.message').html("Found Zipcode"); // Add a message
},
function(errMsg){ // If Zip couldn't be found,
$log.log("error"); // Add an error message
});
};
請指導。 我是否需要創建指令。
在這里,您將其稱為局部函數。
您應該將其稱為$ .zipLookup。
您可以在HTML中添加一個指令。
<input name="zipcode" custDirective></input>
在指令的鏈接函數中,您可以綁定到“模糊”或“單擊”之類的事件。 在這里您可以調用$ .zipLookup()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.