我想在ng-repeat中使用动态ng-model,并希望在控制器中获得这些ng-model的价值。我在谷歌搜索它并使用了stackoverflow的this解决方案。但它在我的案例中给我的定义不明确。我不知道为什么会这样。
我的HTML代码是
<div class="item" ng-repeat="question in quiz_questions">
<p ng-bind-html="question.question"></p>
<div class="dh_yabox">
<div class="placeholder">Your Answer:</div>
<input ng-model="answers[question.question]" class="ansf" type="text" />
</div>
</div>
<div ng-click="nextQuestion()" class="pay_btn btn_defa"><a class="next">Next Question</a></div>
控制器代码
$scope.nextQuestion = function(){
console.log($scope.answers);
angular.element('.slick-next').triggerHandler('click');
}
并且在quiz_questions中是数据
[{"qid":"3","question":"<p>First Quiz Q2</p>\r\n\r\n<p><img alt=\"\" src=\"http://localhost/project/backend/uploads/qt-01.png\" style=\"width: 520px; height: 390px;\" /></p>\r\n","quiz_id":"6","answer":"test,question","time":"2016-09-22 12:43:28","quiz_name":"free quiz","id":"6"},{"qid":"1","question":"First Quiz Q1","quiz_id":"6","answer":"test,test1,test2","time":"0000-00-00 00:00:00","quiz_name":"free quiz","id":"6"}]
任何人都可以告诉我,我在这里做错了什么?
答案 0 :(得分:0)
也许,这是dot
问题。解释here
尝试在您的控制器中使用
$scope.model = {};
$scope.model.answers = {};
和
<div class="dh_yabox">
<div class="placeholder">Your Answer:</div>
<input ng-model="model.answers[question.id]" class="ansf" type="text" />
</div>
因为ng-repeat创建了自己的范围并且它具有绑定性
答案 1 :(得分:0)
这是因为$ scope.answers未在您的控制器中初始化。将其初始化为空对象。
/usr/local/bin/rabbitmqadmin export -u xxx -p xxx home/arte/rabbitmq.config
答案 2 :(得分:0)
几个问题,
ng-model
初始化为$scope.answers = {};
.slick-next
在哪里?console
是否有任何错误。$sce.trustAsHtml();
参见工作示例
var myApp = angular.module('myApp',[]);
myApp.controller('GreetingController', ['$scope', '$sce', function($scope, $sce) {
$scope.answers = {};
$scope.quiz_questions = [{"qid":"3","question":"<b>First Quiz Q2<b/>","quiz_id":"6","answer":"test,question","time":"2016-09-22 12:43:28","quiz_name":"free quiz","id":"6"},{"qid":"1","question":"<b>First Quiz Q1</b>","quiz_id":"6","answer":"test,test1,test2","time":"0000-00-00 00:00:00","quiz_name":"free quiz","id":"6"}];
$scope.nextQuestion = function(){
console.log($scope.answers);
//angular.element('.slick-next').triggerHandler('click');
}
$scope.get_pre = function(x) {
return $sce.trustAsHtml(x);
};
}]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
<div ng-controller="GreetingController">
<div class="item" ng-repeat="question in quiz_questions">
<p ng-bind-html="get_pre(question.question)"></p>
<div class="dh_yabox">
<div class="placeholder">Your Answer:</div>
<input ng-model="answers[question.question]" class="ansf" type="text" />
</div>
</div>
<div ng-click="nextQuestion()" class="pay_btn btn_defa"><a class="next">Next Question</a></div>
</div>
</body>
&#13;