替换ng repeat中的一些符号
Replace some symbols inside of ng-repeat
我正在尝试实现这样的英语测试网页:http://www.englishpage.com/verbpage/verbs20.htm
所以,我在控制器中有JSON:
var studyEng = angular.module('studyEng', []);
studyEng.controller('ExercisesController', function ($scope) {
$scope.exercises = {
'title':'Will / Be Going To',
'description':'Using the words in parentheses, complete the text below with the appropriate tenses, then click the "Check" button to check your answers.',
'exercise':[
{
"question": "Michael: Do you think the Republicans or the Democrats (win) :i: the next election?",
"answerswer":"answerswer 1"
},
{
"question": "Jane: I think the Republicans (win) :i: the next election.",
"answerswer":"answerswer 2"
},
{
"question": "John: No way! The Democrats (win) :i:",
"answerswer":"answerswer 3"
}
]
}
});
和Angular模板:
<div class="starter-template">
<h1>{{exercises.title}}</h1>
<p>{{exercises.description}}</p>
<ul ng-repeat="exercise in exercises.exercise">
<li>{{exercise.question}}</li>
</ul>
</div>
我想做的是改变:I:从我的JSON,到输入字段
求你了,帮帮我——我该怎么做?
附言:我对Angular和StackOverflow也很陌生:)
提前谢谢。
您可以做两件事。
-
你可以把问题分成两部分(假设只有两部分)。
$scope.exercises = { 'title':'Will / Be Going To', 'description':'Using the words in parentheses, complete the text below with the appropriate tenses, then click the "Check" button to check your answers.', 'exercise':[ { "question1": "Michael: Do you think the Republicans or the Democrats (win)", "question2" : "the next election?", "answerswer":"answerswer 1" }, { "question1": "Jane: I think the Republicans (win)", "question2": "the next election.", "answerswer":"answerswer 2" }, { "question1": "John: No way! The Democrats (win)", "question2": "", "answerswer":"answerswer 3" } ]
};
然后将您的模板更改为:
<div class="starter-template">
<h1>{{exercises.title}}</h1>
<p>{{exercises.description}}</p>
<ul ng-repeat="exercise in exercises.exercise">
<li>
<div>{{exercise.question1}}</div>
<input type="text" ng-model="exercise .answer" />
<div>{{exercise.question2}}</div>
</li>
</ul>
</div>
- 您可以将问题保持原样(一个变量),并创建一个自定义角度指令,该指令将按
:i:
分割字符串,并在每个部分之间添加一个答案输入。如果在一个字符串中有多个:i:
,这将无法正常工作。要使用多个答案字段,您必须将answer
更改为列表answers: [answer1: "", answer2: ""]
,并添加相应的答案字段
你几乎已经掌握了。我猜你只想回答其中一个问题。
<div class="starter-template">
<h1>{{exercises.title}}</h1>
<p>{{exercises.description}}</p>
<ul ng-repeat="exercise in exercises.exercise">
<li>{{exercise.question}} <input type="radio" ng-model="answerswer" value="exercise.answer"/></li>
</ul>
</div>
最简单的方法是在回答时使用插值
类似:
function ctrl ($scope, $interpolate) {
$scope.Txt = "This is some text {{Rest}}";
$scope.Rest = "and this is the rest of it.";
$scope.interpolate = function (value) {
return $interpolate(value)($scope);
};
}
<div ng-app ng-controller="ctrl">
<input ng-model="Txt" size="60" />
<p>{{ Txt }}</p>
<p>{{ interpolate(Txt) }}</p>
</div>
我找到了一些解决方案。我做了过滤器:
studyEng.filter ('inputForms', function () {
return function (input) {
var output;
var inputFild = '<input type="text"></input>';
output = input.replace (':i:', inputFild);
return output;
}
});
然后应用它:
<ul ng-repeat="exercise in exercises.exercise">
<li>
<div ng-bind-html="exercise.question | inputForms"></div>
</li>
</ul>
我只是不知道如何只为这个中继器绑定HTML,所以我全局禁用了SCE:
studyEng.config(function($sceProvider) {
// Completely disable SCE. For demonstration purposes only!
// Do not use in new projects.
$sceProvider.enabled(false);
});
相关文章:
- ng映射方向备选方案
- AngularJS UI路由器不能像ng路由器那样工作
- 角度:在ng重复上切换图像
- 正在使用$location.path(.)路由ng视图
- AngularJS:ng之后,重复$scope值未按预期更新
- ng打开空字符串
- 我应该使用Ng提交还是点击表格
- ng init中的表达式无法使用ng repeat
- ng更改事件不适用于Dropdown
- ng视图外的链接重定向到ng视图内的页面
- 在.csv文件中写入学位符号
- ng应用程序使脚本无限运行
- 角度的项目列表 ng 重复,ng 单击显示全宽描述
- 使用相同的数据集绘制各种符号
- ng隐藏和ng显示无法正常工作
- 从ng模板访问作用域
- 防止ng重复中的重复值(AngularJS)
- 替换ng repeat中的一些符号
- 项目符号列表,字母表为ng重复
- 当用作控制器的符号时,使用ng选项进行选择