2017-08-18 58 views
-1

ngModel不工作

$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>

我試圖使用MD-選擇選擇框,但NG-模型似乎並沒有工作。任何想法,我要去哪裏錯了?

ng-change函數中的輸出爲undefined。

+0

我的回答對你有幫助嗎? –

+0

你把它綁定到控制器上了嗎? – Edric

回答

1

使用$scope.modelData,而不是隻modelData

$scope.modelData; 
 
$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>

+0

對不起,這是一個錯字。我在我的代碼中使用了$ scope,它仍然不起作用 –

+0

並且您之前是否使用您的數據定義了'$ scope.modelData'?請分享您的完整代碼。 –

0

是它記錄"undefined"?可能會在控制器$scope.modelName

// 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); 
 
}
<md-select ng-model="modelName" ng-change="callFunction()"> 
 
    <md-option ng-repeat="x in tempData" ng-value="x">{{x}} 
 
    </md-option> 
 
</md-select>

1

更好地工作模型是通過訪問$scope

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

你必須在控制器功能添加$範圍與console.log($scope.modelData);