ngModel无法正常工作

时间:2017-08-18 08:47:29

标签: javascript angularjs angularjs-material



$scope.callFunction = function() {
  console.log($scope.modelData);
}

<md-select ng-model="modelData" ng-change="callFunction()">
  <md-option ng-repeat="x in tempData" ng-value="x">{{x}}
  </md-option>
</md-select>
&#13;
&#13;
&#13;

我尝试使用md-select选择框,但ng-model似乎不起作用。我知道哪里出错了?

ng-change功能的输出是未定义的。

4 个答案:

答案 0 :(得分:1)

使用$scope.modelData代替modelData

&#13;
&#13;
$scope.modelData;
$scope.callFunction = function() {
  console.log($scope.modelData);
}
&#13;
<md-select ng-model="modelData" ng-change="callFunction()">
  <md-option ng-repeat="x in tempData" ng-value="x">{{x}}
  </md-option>
</md-select>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

可以通过控制器$scope

中的$scope.modelName访问模型

&#13;
&#13;
// sometime data might not bind for object in that case you to declare this not always 
$scope.modelName;
$scope.callFunction = function() {
  console.log($scope.modelName);
}
&#13;
<md-select ng-model="modelName" ng-change="callFunction()">
  <md-option ng-repeat="x in tempData" ng-value="x">{{x}}
  </md-option>
</md-select>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

是否正在记录"undefined"?可能会更好地使用console.log($scope.modelData);

答案 3 :(得分:0)

您必须在控制器功能中添加$ scope。

 angular.module('test')
    .controller('TestController',['$rootScope','$http','$scope', 
       function ($rootScope,$http, $scope) {
          $scope.callFunction = function() {
            console.log($scope.modelData);
        }
 }])