在Angular CLI中调用JS函数

时间:2018-02-27 07:48:13

标签: javascript angular angular-cli

我正在尝试在Angular中调用JS函数(API调用)。我首先在HTML / JS中尝试了一切,但是现在在Angular中没有。

在我的app.component.html中,我创建了一个按钮,它应该调用ts文件中的一个函数。

<button (click)="CallFBNewsFeed()">Login with Facebook</button>

在我的app.component.ts中,我得到了应该调用的函数。 第一:

declare var getFBNewsfeed: any;

然后在导出类AppComponent中:

CallFBNewsFeed() {
    console.log("CallNewsFeed")
    getFBNewsfeed.func1();
}

在我在我的ts文件中导入的script.js中,此代码应该执行:

var getFBNewsfeed = (function() {

    return {
        func1: function() {
            function getNewsFeed() {

                FB.api("/ORF/feed", { access_token: 'xxx' }, 
                function (response) {
                    console.log(response)
                        if (response && !response.error) {
                            console.log("Hi")
                            /* handle the result */
                        }
                });
            }
        },

    }

})(getFBNewsfeed||{})

当我运行项目并单击按钮时,出现此错误:

ERROR ReferenceError:未定义getFBNewsfeed堆栈跟踪:[...]

我做错了什么,我怎么能让它发挥作用?谢谢

1 个答案:

答案 0 :(得分:0)

我现在通过将代码保存在html中(其中一切正常)并使用角度中的iframe显示所有内容来“解决”问题。

相关问题