我试图在单选按钮上选择正确的模板,但ng-model值不会传播到$ scope。这是我的控制器:
eventsApp.controller('FirstTestController',
function FirstTestController($scope, $filter) {
$scope.menu = ['List', 'Table'];
//$scope.checkedMenuItem = null;
$scope.data = [
{ action: "Get groceries", complete: false },
{ action: "Call plumber", complete: false },
{ action: "Buy running shoes", complete: true },
{ action: "Buy flowers", complete: false },
{ action: "Call family", complete: false }];
$scope.chooseTemp = function () {
if ($scope.checkedMenuItem == 'Table') {
return 'templates/testTemp.html';
}else {
return 'templates/listTemp.html';
}
};
});
这是我的HTML:
<form name="myForm" ng-controller="FirstTestController">
<div ng-repeat="menuItem in menu">
<label>
<input type="radio" name="group" ng-model="checkedMenuItem" value="{{menuItem}}" />
{{menuItem}}{{checkedMenuItem}} </label>
</div>
<ng-include src="chooseTemp()"></ng-include>
</form>
答案 0 :(得分:0)
由于ng-repeat创建了子范围,因此需要在ng-model中使用$ parent来访问父范围。感谢charlietfl,他的评论帮我解决了这个问题。