我通过 angular.js 开发了一个应用程序 - 小部件,它将托管在 WireCloud 中。我需要获取登录应用程序的用户的用户名。为此,我必须使用 MashupPlatform.context.get('username') - 但 Angular 显然不理解“MashupPlatform”,因为它是一个外部对象。
你知道如何克服这个问题吗?
谢谢
我通过 angular.js 开发了一个应用程序 - 小部件,它将托管在 WireCloud 中。我需要获取登录应用程序的用户的用户名。为此,我必须使用 MashupPlatform.context.get('username') - 但 Angular 显然不理解“MashupPlatform”,因为它是一个外部对象。
你知道如何克服这个问题吗?
谢谢
就像@Meier 所说,当您的小部件在 Wirecloud MashupPlatform 中加载时,会注入,但是要在本地测试它,您可以使用mock-mashupplatform
库,它位于 npm 和 bower 中。目前几乎所有的wirecloud 0.7都得到支持,但本周将支持0.8。
对于此示例,您将执行以下操作:
// Create the default values in a dictionary
var preferencesValues = {
'username': 'myusername'
};
// Construct the default values with strategies
var values = {
'prefs.get': MockMP.strategy.dict(preferencesValues)
};
// Construct the MashupPlatform with that values
window.MashupPlatform = new MockMP.MockMP(values);
当您的小部件部署在 Wirecloud 平台中时,MashupPlatform 将被加载(或注入)。如果你只是想在本地测试它,你可以这样询问它是否存在:
if (typeof MashupPlatform === 'undefined') {
username = "testusername";
}
else {
username = MashupPlatform.context.get('username');
}
最后,这很简单。我的路由有 html 链接,这是错误的。我将路由样式更改为 javascript ( ng-click ),没关系。
感谢大家的帮助。