2016-01-06 45 views
4

我將ng-click $ index值傳遞給我的控制器,然後將它的值用於ng-repeat中的數據但得到一個錯誤提示「ReferenceError:myChoice沒有定義」

我的看法(months.html)有一個所有月份的列表,當它被選中時,它將值傳遞給控制器​​。

<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 

我的控制器具有以下提取代碼:

.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)我想顯示的會議詳情,時間等等。的代碼如下:

<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沒有定義」

請告知我哪裏錯了?謝謝。

+0

應該是'的console.log($ scope.myChoice);',不是嗎? –

+0

謝謝console.log($ scope.myChoice);現在有用。 – GreenDome

回答

5

當你想通過$index使用ng-repeat,你需要指定它。所以,你的HTML應該像如下:

<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 is not defined的原因是因爲你從來沒有初始化一個名爲myChoice變量。但是,您確實有一個名爲$scope.myChoice的變量。
因此,你應該做到以下幾點顯示變量在控制檯:

console.log($scope.myChoice); 
+0

謝謝......我已經按照建議對HTMl進行了更改,但在第二個HTML文件中,我該如何讀取myChoice值?即不起作用... – GreenDome

+1

''不要在視圖中放置'$ scope'。當你訪問'$ scope'變量時,你不會把'$ scope'放在html中。 –

+0

我已經試過了,我得到一個錯誤:「TypeError:v12.months不是一個函數」,這對我沒有多大意義。你知道爲什麼嗎?謝謝。 – GreenDome

相關問題