我正在研究一个单选按钮列表,用户可以从中预先填充问题列表中进行选择,或者选择“其他”单选按钮,然后键入其特定问题。
我可以获取预先填充的单选按钮列表,以工作并解决问题(输出范围变量可以确认这一点),但是引入“其他”功能让我很沮丧。当我选择其他时,它似乎没有绑定到范围变量。我在dom中注意到它缺少class =“ ng-scope”,其他单选按钮似乎是从ng-repeat中获得的,但是我不确定这是否是问题。
<form>
// This part loops through the list of problems and makess radio buttons
<div ng-repeat="problem in selectedType['nature_of_problem']">
<input type="radio" ng-model="$parent.natureOfProblem" ng-value="problem"/>
</div>
// Ideally this part is where the "other" radio is, it's still in the form
<input type="radio" ng-model="natureOfProblem" ng-value="other" ng-checked="">
</form>
工作的JSFiddle:
http://jsfiddle.net/HB7LU/3794/
我看到了一些问题,其中包括:
希望这可以帮助!
function MyCtrl($scope) {
$scope.uiState = {};
$scope.uiState.natureOfProblem = 1;
$scope.selectedType = {};
$scope.selectedType.nature_of_problem = [1,2,3];
}
<div ng-controller="MyCtrl">
<p>Nature of problem is: {{uiState.natureOfProblem}}</p>
<form>
<div ng-repeat="problem in selectedType['nature_of_problem']">
<input type="radio" ng-model="uiState.natureOfProblem" ng-value="problem"/><span ng-bind="problem"></span>
</div>
<input type="radio" ng-model="uiState.natureOfProblem" value="Other" /><span>Other</span>
</form>
</div>
编辑以回答OP的问题:
我倾向于不习惯使用ng-bind -在速度较慢的浏览器(如Firefox)中,当加载所有内容时,它不会在屏幕上显示“ {{blah}}”。较新版本的Angular也为此目的提供了ng-cloak,我应该养成使用它的习惯。:)(我也隐约记得读过“ {{blah}}”会引起IE中的问题,但我很可能对此做了弥补。)
点表示法的使用与Angular无法在全新对象上维护数据绑定有关。尝试不使用“作用域”和“继承”之类的术语来解释它:如果通过更改yourObject.anAttribute影响现有对象,则整个过程中一致存在的总体对象不会删除其绑定。但是,如果blahVariable等于8,并且将blahVariable设置为7,则基本上已经将旧数据扔掉了,并完全创建了新数据。这个新片段不会保持绑定,因此控制器从不会从值已更改的视图中获取备忘录。
有时,我发现这实际上很有用-您可以出于某些快速目的而在视图中短暂地操纵变量,而无需控制器来发现它。:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句