我发现很多人都有同样的问题,但给出的答案对我不起作用。
这是我的代码:
<div ng-controller="addEventController">
//other form input fields that will be sent when clicking the button
<div ng-controller="imgListCtrl">
<input ng-repeat-start="image in images" name = "selectImage" type="radio" ng-
model="img" ng-value="{{image.imageid}}"/>
<img class="images" ng-src="{{image.url}}" width="50" height="50"/>
<br ng-if="($index+1) % 10 == 0"/><br ng-if="($index+1) % 10 == 0"/>
<span ng-repeat-end></span>
</div>
<button class="btn-default" ng-click="saveEvent()">Opslaan</button>
</div>
这是'addEventController'的子控制器,在父控制器中我尝试访问所选的单选按钮值,如下所示:
myAppProfile.controller('addEventController', function($scope,$location, $http) {
$scope.saveEvent = function() {
$http({
method: 'POST',
url: 'eventController.php',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: {
'begin': $scope.begin + " " + $scope.btijd,
'einde': $scope.einde + " " + $scope.etijd,
'beschrijving': $scope.beschrijving,
'img': $scope.img
}
}).
success(function(data, status) {
if(data == "1"){
$location.path("/agenda");
}else{
$scope.errormsg = data;
}
}).
error(function(data, status) {
$scope.errormsg = data;
});
}
});
$ scope.img =&gt;总是返回“未定义”。
我发现你必须将ng-model对象重命名为$ parent.img,或者你必须将它命名为images.img,我已经尝试了我在网上找到的所有答案,但在我的情况下,总是未定义。任何人都知道如何获得所选放射性按钮的值?
答案 0 :(得分:3)
发布后立即找到答案,也许它可以帮助某人。仅仅执行$ parent.img是不够的,因为它只会进入子控制器的范围,但我不得不这样做:ng-model =“$ parent。$ parent.img”转到父控制器。
答案 1 :(得分:0)
为什么不在addEventController上定义一个scope属性并提升子范围属性?
<div ng-controller="addEventController">
//other form input fields that will be sent when clicking the button
<div ng-controller="imgListCtrl">
<input ng-repeat-start="image in images" name = "selectImage" type="radio" ng-
model="event.img" ng-value="{{image.imageid}}"/>
<img class="images" ng-src="{{image.url}}" width="50" height="50"/>
<br ng-if="($index+1) % 10 == 0"/><br ng-if="($index+1) % 10 == 0"/>
<span ng-repeat-end></span>
</div>
<button class="btn-default" ng-click="saveEvent()">Opslaan</button>
</div>
myAppProfile.controller('addEventController', function($scope,$location, $http) {
$scope.event = {};
$scope.saveEvent = function() {
$http({
method: 'POST',
url: 'eventController.php',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: {
'begin': $scope.event.begin + " " + $scope.event.btijd,
'einde': $scope.event.einde + " " + $scope.event.etijd,
'beschrijving': $scope.event.beschrijving,
'img': $scope.event.img
}
}).
success(function(data, status) {
if(data == "1"){
$location.path("/agenda");
}else{
$scope.errormsg = data;
}
}).
error(function(data, status) {
$scope.errormsg = data;
});
}
});