【问题标题】:AngularJS ng-options not binding to ng-modelAngularJS ng-options 未绑定到 ng-model
【发布时间】:2017-03-16 01:44:38
【问题描述】:

这里有类似的问题,但我仍然无法弄清楚我做错了什么。我有 ng-options 在表单中填充标签。在表单的提交控制器中,当我 console.log ng-model 时它返回未定义。如有任何帮助,我将不胜感激。

<label ng-controller="educationController">
    select your level of education
    <select ng-model="selectedEducation" ng-options="education for education in degrees" required>
    </select>
  </label>

这是控制器

angular.module('drsApp.reviewerApplication');
app.controller('educationController', educationController)
function educationController($scope){
$scope.degrees=[
  "Bachelor Degree",
  "Master Degree",
  "Doctorate Degree"
  ]
};

这是在表单的控制器中

console.log("education: "+ $scope.selectedEducation);

【问题讨论】:

    标签: angularjs-ng-model angularjs-ng-options


    【解决方案1】:

    显示未定义是因为你写错了 ng-model 名称。ng-model 名称应该是“selectedEducation”而不是控制台中的“education”--

    console.log("教育:"+ $scope.selectedEducation);

    你可以在这里查看--

    <!DOCTYPE html>
    <html>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    
    <body>
    
        <div ng-app="myApp" ng-controller="educationController">
    
            select your level of education
            <select ng-model="selectedEducation" ng-options="education for education in degrees" required>
            </select>
            <button ng-click="formSubmit()">Submit</button>
        </div>
    
        <script>
            var app = angular.module('myApp', []);
            app.controller('educationController', function ($scope) {
                $scope.degrees=[
                  "Bachelor Degree",
                  "Master Degree",
                  "Doctorate Degree"
    			      ];
                $scope.formSubmit=function (){
                  console.log("education: "+ $scope.selectedEducation);
                }
            });
    		
        </script>
    
        
    
    </body>
    
    </html>

    【讨论】:

    • 我不完全确定为什么,但我不能这样定义我的控制器。 “ var app = angular.module('myApp', []); app.controller('educationController', function ($scope)" 甚至当我更正了 console.log 时,它仍然未定义。我实际上在之后发现了那个错误提交问题。
    • @Jen 然后复制你那部分的代码并编辑你的问题,没有看到你的代码就无法理解问题出在哪里
    • 感谢您的帮助。我更新了表单控制器。我必须像我一样声明教育控制器,否则分页符
    • @Jen 太好了。如果我的回答对您有帮助,请接受我的回答。
    • 对不起,你一定误会了。它仍然无法正常工作。我对原始问题进行了更新。它仍然无法正常工作,但如果我使用普通的 javascript,我可以让它工作。
    猜你喜欢
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2013-03-13
    • 2015-03-11
    • 2016-11-18
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多