0
每次有人點擊答案並執行data-ng-click =「addAnswer(questionId,0)」時,$ scope.answers數組將被覆蓋。我不確定它爲什麼這樣做。Angularjs推式覆蓋範圍陣列
難道是因爲每次新模板被加載到$ scope中都會被重置?如果是這樣,那不是我期望的行爲。
感謝您的協助。
的index.html(縮寫)
<div id="q" class="cta1_content ugh" data-ng-controller="testYourself">
<div ng-view></div>
</div>
測試yourself.html
<div>
<div class="row">
<h1 class="text-center">{{question.name}}</h1>
<div class="col-xs-6 col-md-3 col-md-offset-3 text-center yesno">
<a href="#/test/{{questionId+1}}" class="q" data-ng-click="addAnswer(questionId, 1)">
<span class="cta_next"><i class="icon ion-checkmark-round"></i></span>
</a>
</div>
<div class="col-xs-6 col-md-3 text-center yesno">
<a href="#/test/{{questionId+1}}" class="q" data-ng-click="addAnswer(questionId, 0)">
<span class="cta_next"><i class="icon ion-close-round"></i></span>
</a>
</div>
</div>
</div>
app.js
var calculonApp = angular.module('calculonApp', [
'ngRoute',
'calculonControllers',
'ui.bootstrap.showErrors'
]);
calculonApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/test/:questionId', {
templateUrl: 'app/partials/test-yourself.html',
controller: 'testYourself'
}).
otherwise({
redirectTo: '/test/0'
});
}]);
controller.js
calculonControllers.controller('testYourself', ['$scope', '$routeParams',
function($scope, $routeParams) {
$scope.quiz = [
{name:"a", answer: [{0: '1.', 1: '2'}], weight:25},
{name:"b", answer: [{0: '1', 1: '2'}], weight:25}
];
$scope.question = $scope.quiz[$routeParams.questionId];
$scope.questionId = parseInt($routeParams.questionId);
$scope.answers = [];
$scope.addAnswer = function(a) {
$scope.answers.push({
'question':$scope.questionId,
'answer':a
});
};
}]);
爲什麼爲'a'定義'href'。你爲什麼想瀏覽。 – Chandermani 2014-10-09 11:52:55