繁体   English   中英

从AngularJS控制器调用Javascript函数

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM