我创建了一个带有角度2的NativeScript应用程序,我有一个对象数组,我期望在应用程序的前端看到.行为是,如果我直接在ngOnInit()内部将对象推入数组,它可以工作,但如果我在ngOnInit()中创建一个承诺它不起作用.这是代码:
export class DashboardComponent {
stories: Story[] = [];
pushArray() {
let story:Story = new Story(1,1,"ASD", "pushed");
this.stories.push(story);
}
ngOnInit() {
this.pushArray(); //this is shown
var promise = new Promise((resolve)=>{
resolve(42);
console.log("promise hit");
});
promise.then(x=> {
this.pushArray(); //this is NOT shown
});
}
}
Run Code Online (Sandbox Code Playgroud)
相对的html是:
<Label *ngFor="let story of stories" [text]='story.message'></Label>
Run Code Online (Sandbox Code Playgroud)
当应用程序启动时,我只看到一次推送,但是我创建了一个触发"console.log(JSON.stringify(this.stories))"的按钮;" 在那一刻,当我点击按钮时,ui似乎检测到更改的数组,并出现另一个推送的对象.
编辑:
我在这个帖子中创建了一个更简单的例子:Angular 2:当我在ngOnInit中更改promise.than中的变量时,视图不会刷新
您好我想知道如何在nativescript中设置设备方向.具体来说,我希望我写的应用程序始终保持相同的方向(纵向),以便旋转设备不会导致它进入横向.
我尝试了nativescript-orientation插件和setOrientation.
var orientation = require('nativescript-orientation');
console.log(JSON.stringify(orientation));// outputs JS: {}
orientation.setOrientation("portrait");
Run Code Online (Sandbox Code Playgroud)
但是我收到错误"无法读取属性setOrientation of undefined.tns插件列表显示插件已安装.此外,我尝试删除platforms/android目录并运行tns platform add android相同的结果.
我也尝试将各种组合android:screenOrientation="portrait"放入AndroidManifest.xml但没有成功.
App_resources内部的AndroidManifest.xml看起来像这样
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="__PACKAGE__"
android:versionCode="1"
android:versionName="1.0">
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"/>
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="__APILEVEL__"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:screenOrientation="portrait"
android:name="com.tns.NativeScriptApplication"
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="com.tns.NativeScriptActivity"
android:label="@string/title_activity_kimera"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@style/LaunchScreenTheme">
<meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.tns.ErrorReportActivity"/>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud) 当我的布局里面装载它的宽度和高度的任何观点NaN,它也有一个getMeasuredHeight()和getMeasuredWidth()的0.
在某些时候getMeasuredHeight()和getMeasuredWidth()(布局在布置完我猜的)收到有用的值.
我怎样才能获得任何尺寸?我该怎么看他们改变?什么时候会.width和.height以往任何时候都不能NaN??? 为什么我不能将视图悬停在整个屏幕上????
到目前为止,我每100毫秒进行一次投票,我觉得很愚蠢.请帮忙.
我正在使用来自Telerik的NativeScript,我使用调试名称("notiApp")创建了一个应用程序,但现在我无法在启动器和操作栏中更改应用程序的名称.
我已经尝试配置我AndroidManifest.xml的/app/App_Resources/Android/修改标签的android:label属性<application>.
<?xml version="1.0" encoding="utf-8"?>
<manifest>
<application
android:label="DESIRED NAME">
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
此外,我尝试更改我的项目的根目录名称为新的应用程序所需的名称.
还有什么我可以做的或者我可以提供的任何其他信息?
先谢谢你们!
我需要开发一个应用程序,该应用程序应定期检查可用的WiFi网络,并在特定网络在范围内时触发操作.
我计划实现一个Android服务来处理它不断检查可用网络的部分,并在检测到特定网络后广播通知.
我想知道如何将它集成到NativeScript应用程序上,我的意思是我想让NativeScript订阅的代码接收来自Android服务的通知,并且一旦收到它就做了一些事情.
为了清楚起见,我希望服务是本机的(用java编写)并以某种方式将它包含在我的NativeScript应用程序中,以使其与GUI交互.
谢谢,
我正在学习如何使用NativeScript编写应用程序.我相信最好的学习方法就是做.出于这个原因,我正在构建一个基本的应用程序.
在这个应用程序中,我正在尝试创建一个函数和一个变量,我可以访问所有视图模型和应用程序中的其他代码.为了做到这一点,我想我会在应用程序对象上添加一个函数和变量.
在NativeScript中,使用以下代码初始化应用程序:
app.js
var application = require("application");
application.mainModule = "main-page";
application.start();
Run Code Online (Sandbox Code Playgroud)
我想我可以捎带这个并添加一个全局可见的函数和变量,如下所示:
application.prototype.myFunction = function() {
console.log('I made it!');
};
application.myVariable = 'some value';
Run Code Online (Sandbox Code Playgroud)
然后,在我的视图模型或其他代码中,我可以执行以下操作:
意见/ home.js
application.myFunction();
console.log(application.myVariable);
Run Code Online (Sandbox Code Playgroud)
但是,当我运行此代码时,我收到一个错误,指出应用程序未定义.我不完全理解这一点.我认为,因为应用程序在app.js中定义/实例化,它将全局可见.但是,它似乎不是.与此同时,我不知道该怎么做.
我试图在NativeScript中使用这个ios-charts库.这个库是用Swift编写的,而不是用Objective-C编写的.我可以用吗?我试过用它但它给了我一个错误.我在以下步骤中使用它:
我已经使用我的nativescript项目添加了库
tns library add ios 'library_path'
Run Code Online (Sandbox Code Playgroud)
库已添加.然后我用ios平台准备了项目
tns prepare ios
Run Code Online (Sandbox Code Playgroud)
为了测试是否成功添加了库,我构建了项目Xcode,它构建成功,但是在运行时我得到以下错误.
dyld: Library not loaded: @rpath/libswiftCore.dylib
Referenced from: /Users/UserNameHere/Library/Developer/Xcode/DerivedData/Build/Products/Debug-iphonesimulator/Charts.framework/Charts
Reason: image not found
Run Code Online (Sandbox Code Playgroud)
我摆脱了这个错误,当我设置Embedded content contains swift code到YES.但现在当我尝试访问库时
var charts = new Charts();
Run Code Online (Sandbox Code Playgroud)
它给出了如下参考错误:
ReferenceError: Can't find variable: Charts
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下方法访问图表:
1. var charts = new Charts.Swift();
2. var charts = new Charts.Charts();
3. var charts = new Charts-Swift();
4. var charts = new Charts.LineChartView();
5. var charts = require("Charts");
6. …Run Code Online (Sandbox Code Playgroud) 我想通过Native Script在iOS中使用3D触控支持.目前有一个快速动作插件,但我想听听ForceTouchiOS设备的动作.我想使用本机iOS API执行此操作,但我不知道如何开始.同一作者
还有另一个Cordova插件,其中有一个函数,我们可以听取下面的强制触摸:
ThreeDeeTouch.watchForceTouches(function(result) {
console.log("force touch % " + result.force); // 84
console.log("force touch timestamp " + result.timestamp); // 1449908744.706419
console.log("force touch x coordinate " + result.x); // 213
console.log("force touch y coordinate " + result.y); // 41
});
Run Code Online (Sandbox Code Playgroud)
如何在NativeScript iOS中访问3D Touch API?
在listview项目中,我Visiblity在布局中使用概念来执行可见和折叠.执行时Collapse,listview项目不会完全从布局中删除该视图.
它正在删除名称和ID等项目内容,但在listview中的特定listitem位置放置空白白色视图.
下面我分享了代码以便更好地理解:
StudentData.ts:
export class StudentData {
constructor(public id: number, public name: string, public collapseData: boolean) {}
}
Run Code Online (Sandbox Code Playgroud)
student.page.html:
<ListView id="listId" [items]="allFeedItems" class="list-group" height="300">
<ng-template let-item="item">
<StackLayout [visibility]="item.collapseData ? 'visible' : 'collapse'" >
<StackLayout orientation="horizontal">
<Label class="item-address" text="address"></Label>
</StackLayout>
.....
</StackLayout>
</ng-template>
</ListView>
Run Code Online (Sandbox Code Playgroud)
怎么了:
例如:在模态类中,我在hashmap中保存listitems的开关控制值.当回到我的主页(即)StudentPage时,我需要完全隐藏特定的行项.但它只删除内容名称和ID.它不会删除该特定列表视图项位置的空白视图.
我期待的是:
删除listview中该特定项目位置的空白视图.
我有一个Javascript函数(在基于Angular 2 NativeScript的移动应用程序中),按下按钮时触发,它应该隐藏按钮并在其位置显示活动指示器,执行蓝牙扫描,完成后关闭活动指示灯并显示原始按钮.
bluetoothAdd() {
this.isScanning = true;
var plusIcon = this.page.getViewById("add");
plusIcon.style.opacity = 0;
bluetooth.hasCoarseLocationPermission().then(
function (granted) {
if (!granted) {
bluetooth.requestCoarseLocationPermission();
} else {
bluetooth.startScanning({
serviceUUIDs: ["133d"],
seconds: 4,
onDiscovered: function (peripheral) {
console.log("Periperhal found with UUID: " + peripheral.UUID);
}
}).then(function () {
console.log("scanning complete");
this.isScanning = false;
plusIcon.style.opacity = 1;
}, function (err) {
console.log("error while scanning: " + err);
});
this.isScanning = false;
}
});
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,该this.isScanning = false;行会抛出所有这些错误.我做错了什么?
CONSOLE ERROR file:///app/tns_modules/angular2/src/platform/server/parse5_adapter.js:55:75: EXCEPTION: …Run Code Online (Sandbox Code Playgroud) nativescript ×10
android ×3
javascript ×3
angular ×2
angularjs ×1
es6-promise ×1
ios ×1
swift ×1
telerik ×1
typescript ×1
xml ×1