【问题标题】:how to reach to a variable in another js file in appcelerator alloy如何在appcelerator合金中访问另一个js文件中的变量
【发布时间】:2014-02-12 12:30:40
【问题描述】:

我有一个小问题。

我有 index.js

var loc = require('location');
function doClick (){
loc.doIt();
}

在 location.js 我有这些

var dee  = 12;
exports.doIt = function() {
    alert(dee);
};

这意味着当我单击按钮时,我可以获得警报,但是,我想在不需要单击的情况下访问这些信息 - onLoad - 此外,我想返回两个值而不仅仅是一个。 我该如何解决这个问题也许它确实有一个简单的解决方案,但是因为我已经工作了一段时间,我的大脑停止了工作:)

问候

【问题讨论】:

    标签: variables appcelerator onload titanium-alloy


    【解决方案1】:

    你应该把你的 location.js 移到 app/lib 里面(作为模块)。例如:

    // app/lib/helper.js
    exports.callAlert = function(text) {
       alert('hello'+ text);
    }
    

    然后像这样在你的控制器中调用它:

    var helper = require("helper"); // call helper without path and .js extension
    helper.callAlert('Titanium');
    

    你的问题应该得到解决:)

    【讨论】:

      猜你喜欢
      • 2018-09-26
      • 1970-01-01
      • 2017-02-06
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-07
      • 2021-03-26
      相关资源
      最近更新 更多