【问题标题】:Angularjs ng-repeat radio button list: selected radio returning undefined valueAngularjs ng-repeat单选按钮列表:选择的单选返回未定义的值
【发布时间】:2014-04-11 07:38:03
【问题描述】:

我发现很多人都有同样的问题,但给出的答案对我不起作用。

这是我的代码:

<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}}"/>&nbsp;
      <img class="images" ng-src="{{image.url}}" width="50" height="50"/>&nbsp;
      <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 => 总是返回“未定义”。

我发现您必须将 ng-model 对象重命名为 $parent.img 或者您必须将其命名为 images.img,我已经尝试了我在网上找到的所有答案,但就我而言,始终未定义。任何人都知道如何获取所选单选按钮的值?

【问题讨论】:

    标签: angularjs radio-button undefined angularjs-ng-repeat


    【解决方案1】:

    发布后立即找到答案,也许它可以帮助某人。做 $parent.img 是不够的,因为这只涉及子控制器的范围,但我必须这样做: ng-model="$parent.$parent.img" 去到父控制器。 .

    【讨论】:

    • 这是因为 ng-repeat 创建了一个新的子范围,因此第一个 $parent 上升到您的子控制器。
    【解决方案2】:

    为什么不在 addEventController 上定义作用域属性并提升子作用域属性?

    <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}}"/>&nbsp;
      <img class="images" ng-src="{{image.url}}" width="50" height="50"/>&nbsp;
      <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;
        });
    
    }
    

    });

    【讨论】:

      猜你喜欢
      • 2015-06-27
      • 2022-11-11
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 2016-05-24
      • 2014-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多