角JS - NG-点击$指数值传递给控制器功能,然后再返回到其他视图 [英] Angular JS - Passing ng-click $index value to a controller function then back to another view
问题描述
我是NG-点击$指数值传递给我的控制器,然后使用它在NG-重复我的数据从一个数组值,但得到一个错误提示的ReferenceError:myChoice没有定义
I am pass the ng-click $index value to my controller then using it's value in an ng-repeat to my data from an array but getting an error suggesting "ReferenceError: myChoice is not defined"
我的观点(months.html)的所有月份的列表,选中时,它的价值传递给控制器。
My view (months.html) has a list of all the months, when selected, it passes value to the controller.
<ion-list ng-repeat="time in times.months" ng-click="getMonthId($index)" style="">
<ion-item style="" href="#/dmtimes">{{ time.monthId }} - {{ time.MonthName }</ion-item>
</ion-list>
视图输出显示:
1 - Jan
2 - Feb
3 - Mar
etc
我的控制器有以下摘录code:
My controller has the following extract code:
.controller('dailyMeetingTimesCtrl', function($scope, MeetingNames, $ionicLoading, $firebase, $firebaseArray) {
$scope.getMonthId = function (monthId) {
alert("you selected: " + monthId); // this works fine.
$scope.myChoice = monthId; // ReferenceError: myChoice is not defined
console.log(myChoice);
}
})
在我的第二个观点(displayMeetingTimes.html)我要显示会议的详细信息,时间等为选定的一个月。在code是如下:
In my second view (displayMeetingTimes.html) I want to display the meeting details, times etc for the selected month. The code is as follows:
<ion-view title="Daily Meeting Times">
<ion-content ng-controller="dailyMeetingTimesCtrl" class="has-header" overflow-scroll="true" padding="true">
<ion-list>
<ion-item ng-repeat="time in times.months(myChoice).days" class="item-text-wrap">
<span>{{myChoice}}</span>
Meeting Name: {{ time.meetingName }}<br />
Star Time: {{ time.startTime }}<br />
Finish Time: {{ time.finishTime }}<br />
</ion-item>
</ion-list>
</ion-content>
</ion-view>
当我跑的应用,myChoice不通过传递给第二个观点,因此错误的ReferenceError:myChoice没有定义值
When I ran the application, the value of 'myChoice' is not passing through to the second view hence the error "ReferenceError: myChoice is not defined"
请指教我要去哪里错了吗?谢谢你。
Please advise where I'm going wrong? Thank you.
推荐答案
当你想通过 $指数
使用 NG-重复
,你需要指定它。所以,你的HTML应该像下面这样:
When you want to pass the $index
using ng-repeat
, you need to specify it. So your HTML should be like following:
<ion-list ng-repeat="time in times.months track by $index" ng-click="getMonthId($index)" style="">
<ion-item style="" href="#/dmtimes">{{ time.monthId }} - {{ time.MonthName }
</ion-item>
</ion-list>
其次,为什么你得到的ReferenceError的原因:myChoice没有定义
是因为你从来没有初始化一个名为 myChoice变量
。但是你有一个名为 $ scope.myChoice
变量。
因此,你应该做到以下几点显示在控制台中的变量:
Secondly, the reason why you are getting ReferenceError: myChoice is not defined
is because you have never initialized a variable called myChoice
. However you do have a variable called $scope.myChoice
.
Therefore, you should do the following for display the variable in the console:
console.log($scope.myChoice);
这篇关于角JS - NG-点击$指数值传递给控制器功能,然后再返回到其他视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!