如何使用 AngularJS 访问不同 js 文件中的变量数据



我想在另一个js文件中使用变量数据。我如何在 anularjs 中做到这一点。

在一个.js文件中,我有一个变量tagText="SAP",我想在另一个js文件(two.js(中使用它的值。如何在 angularjs 中做到这一点?

谁能帮忙?

据我的观点您可以创建一个服务.js

myApp.service('oneService', oneService);
function oneService(){
    var tagText;
    return {
        getTagText:getTagText,
        setTagText:setTagText
    };
    function getTagText(){
        return tagText;
    }
    function setTagText(tagText){
        this.tagText=tagText;
    }
}
在任何控制器

中注入此服务,您可以通过getter和setter访问该控制器上的tagText变量

单例中的服务角度,一次您有最新的变量值

最新更新