对 Angular 相当陌生,从 Controller 获取数据并使用 ionic 在 View 中显示时遇到一些问题。
我在 Controller (“DashCtrl”)中有一个函数,我使用 ng-click 传递一个字符串以在该函数中使用。现在我只想获取这个字符串并将其显示在下一个 View 中,因为我在具有 ng-click 事件的同一元素上有一个 href。
当我在下一页上记录字符串时,它正在工作并显示出来。我如何在 View 中使用它?
代码如下:
HTML
*首页
<ul contentful-entries>
<div ng-repeat="story in $contentfulEntries.items">
<li class="story col col-100">
<a href="#/tab/story/{{story.fields.slug}}" ng-click='storyDetails("{{story.fields.title}}")'>
<p>{{ story.fields.title }}</p>
</a>
</li>
</div>
</ul>
*点击 ng 时重定向到此
<ion-view view-title="Story">
<ion-nav-buttons side="left">
<button class="button back-button buttons button-clear header-item" ng-click="goBack()">
<i class="icon ion-ios-arrow-back"> Back</i>
</button>
</ion-nav-buttons>
<ion-content>
<ion-list ng-controller="DashCtrl">
<h1>{{ story.slug }} </h1>
<p> <!-- HERE IS WHERE I WANT TO DISPLAY THE STRING --> </p>
</ion-list>
</ion-content>
</ion-view>
*这是我的 Controller
.controller('DashCtrl', function($scope, $stateParams, $ionicHistory) {
$scope.storyDetails = function($scope, details) {
// here is the data i need to display in the view
// the console.log shows up in the browser
console.log(details);
}
$scope.goBack = function(){
$ionicHistory.goBack();
}
// this is working fine
$scope.story = $stateParams;
})
最佳答案
将详细信息字符串保存为 Controller 变量,然后您可以在 View 中访问它。
关于javascript - 通过 ng-click Angular 使用 Controller View 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39372593/