【发布时间】:2017-03-19 17:03:00
【问题描述】:
** 更新**
在我的代码中,我有一张卡片,其中包含视图中作业数组的当前索引。这样,用户可以使用手势将卡片从屏幕上滑出,然后将显示作业数组中的下一个索引。
在我的组件中
@ViewChild("card") card: ElementRef;
@ViewChild("job") job: ElementRef;
let jobsArray = ["Developer","Food Editor","Journalist","Actor","Fisherman","Chef","Designer","Musician","Baker"];
this.job.nativeElement = jobsArray[i];
这样我就设置好了对象,所以当刷卡时,这个函数内部会处理,
this.card.nativeElement.on(GestureTypes.swipe, function (args) {
// DO stuff
}
我的问题是,
我无法从作业数组中获取任何数据以显示在视图中。我正在尝试使用我在网上可以找到的东西来完成这项工作,但我不确定我是否走在正确的轨道上。
我使用正确吗
@ViewChild("card") card: ElementRef;
@ViewChild("job") job: ElementRef;
** 结束更新 **
背景
我正在努力学习编写 NativeScript。我有使用 Angular2 的经验,所以我认为它会很简单。
我找到了一个如何从视图中滑动数组的示例,但它位于核心 NativeScript 中。
我正在尝试将其转换为 Angular,但我很确定我没有正确绑定数据。
示例
在视图中,
<ActionBar title="Stream" class="action-bar">
<ActionItem text="Menu" ios.position="left"></ActionItem>
<ActionItem text="Group" ios.position="right"></ActionItem>
</ActionBar>
<FlexboxLayout flexDirection="column" class="stream">
<StackLayout id="card" class="card">
<Label id="job"></Label>
</StackLayout>
</FlexboxLayout>
所以我在那个例子中意识到这不是我通常将数据添加到 Angular2 视图的方式。所以我尝试了这种添加数据的变体,
<StackLayout card="card" i="index" #card [card]="card" id="card" class="card">
在示例最初使用的组件中,
var verdict = _page.getViewById("verdict");
但我读过在 nativescript 中使用 angular2 时,你会这样做,
@ViewChild("job") job: ElementRef;
let jobsArray = ["Developer","Food Editor","Journalist","Actor","Fisherman","Chef","Designer","Musician","Baker"];
this.job.nativeElement = jobsArray[i];
基本上我想要完成的是在我看来有一个卡片部分。然后一次显示卡片中作业数组的一个索引。因此,当我滑动视图时,每次我从视图上刷卡时,它都会在作业数组中移动。
这是我现在拥有的组件,
export class StreamComponent implements OnInit {
@ViewChild("card") card: ElementRef;
@ViewChild("job") job: ElementRef;
constructor(private router: Router) { }
ngOnInit(): void {
this.singleCard();
}
singleCard() {
let jobsArray = ["Developer","Food Editor","Journalist","Actor","Fisherman","Chef","Designer","Musician","Baker"];
let i = 0;
this.card.nativeElement.on(GestureTypes.swipe, function (args) {
this.job.nativeElement = jobsArray[i];
i = i + 1;
if(args.direction === 1){
console.log("You liked it, save to your favs!")
this.card.animate({ translate: { x: 1000, y: 100 } })
.then(function () { return this.card.animate({ translate: { x: 0, y: -2000 } }); })
.then(function () { return this.card.animate({ translate: { x: 0, y: 0 } }); })
.then(function () {
this.job.text = jobsArray[i];
})
.catch(function (e) {
console.log(e.message);
});
}
else{
console.log("You hate it, get rid of that shit!")
this.card.animate({ translate: { x: -1000, y: 100 } })
.then(function () { return this.card.animate({ translate: { x: 0, y: -2000 } }); })
.then(function () { return this.card.animate({ translate: { x: 0, y: 0 } }); })
.then(function () {
this.job.text=jobsArray[i];
})
.catch(function (e) {
console.log(e.message);
});
}
});
}
}
问题
使用
将数据绑定到视图this.job.nativeElement
在视图中一次显示一张卡片的数据。
【问题讨论】:
-
你需要实现一些类似惰性获取的东西吗?
-
我不熟悉惰性获取。那是什么?基本上,我正在尝试获取一张卡片来保存作业数组中的单个索引。然后当用户滑动屏幕或卡片时,它将更改为作业数组中的下一个索引。我的问题是将数据附加到卡内,然后让卡显示在视图中。我对什么是核心 nativescript 和 angular 中使用的内容感到困惑。我知道 ng-2,但第一次使用 nativescript。这就是我想要做的,但这是在核心 nativescript 中。 nativescriptsnacks.com/snippets/2016/06/07/swipable-cards.html
-
在大多数情况下,您可以使用 angular2 指令,例如 ngif .. 但主要区别在于移动视图。我相信对于您的情况,您应该使用 listview。有一些选择。我将发布代码。
-
你太棒了,谢谢。您的示例将有很大帮助!
-
<ListViewLinearLayout tkListViewLayout scrollDirection="Horizontal" itemWidth="80"></ListViewLinearLayout>查看使用列表视图的答案。但是您需要更改滚动方向您可以在tkListItemTemplatestackoverflow.com/questions/42731622/… 中实现的任何内容
标签: angular nativescript angular2-nativescript