Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript angularjs-指令在函数中不起作用_Javascript_Angularjs_Function_Angularjs Directive - Fatal编程技术网

Javascript angularjs-指令在函数中不起作用

Javascript angularjs-指令在函数中不起作用,javascript,angularjs,function,angularjs-directive,Javascript,Angularjs,Function,Angularjs Directive,我尝试使用angular指令,如果我正常地将其放到js文件中,它可以正常工作,但如果我尝试将其放到函数中,它就不工作了: function getCity(city){ document.getElementById("weather").innerHTML=city; $.get('https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select w

我尝试使用angular指令,如果我正常地将其放到js文件中,它可以正常工作,但如果我尝试将其放到函数中,它就不工作了:

function getCity(city){
    document.getElementById("weather").innerHTML=city;
    $.get('https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="' + city + '")&format=json', function (data) {
       humi = data.query.results.channel.atmosphere.humidity;
    });
    var app = angular.module("weatApp", []);
    app.directive("humiGet", function() {
    return {
        template : [humi]
    };
    });
}

在调用指令时,可能无法设置humi:

function getCity(city){
    document.getElementById("weather").innerHTML=city;
    $.get('https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="' + city + '")&format=json', function (data) {
        humi = data.query.results.channel.atmosphere.humidity;

        var app = angular.module("weatApp", []);
        app.directive("humiGet", function() {
           return { template : [humi] };
        });
    });
}
更好的方法是使用:


getCity是否在任何地方被调用?您一定忘了调用getCity()。请检查。它被调用并记录。getElementById(“天气”)。innerHTML=city;按调用的方式工作,但指令不工作。为什么使用jquery调用API?相反,您可以在Angular本身中使用$http服务调用。还有,您的控制台中有哪些错误当我搜索API时,我找到了jquery的示例,所以我使用了它,我对Angular不太熟悉。我得到的一个错误是未捕获错误:[$injector:modulerr]…ogleapis.com%2Fajax%2Flibs%2angularjs%2F1.4.8%2fagnal.min.js%3A20%3A274)在angular.js:38在angular.js:4458在n(angular.js:340)在g(angular.js:4419)在eb(angular.js:4344)在c(angular.js:1676)在yc(angular.js:1697)在Zd(angular.js:1591)在HTMLDocument.b(angular.js:3057)的angular.js:29013中,它不可能是它,因为如果我更改return{template:“whatever”},它仍然不会在该函数中返回任何内容
function getCity(city){
    document.getElementById("weather").innerHTML=city;
    var deferred = $.Deferred();
    $.get('https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="' + city + '")&format=json', function (data) {
        humi = data.query.results.channel.atmosphere.humidity;
        var app = angular.module("weatApp", []);
        app.directive("humiGet", function() {
           return { template : [humi] };
        });
        deferred.resolve(true);
    });

    return deferred.promise();
}

// else where
getCity('Fargo').then(function () {
    // use directive here
});