范围模型显示未定义的值

时间:2015-12-17 15:25:55

标签: angularjs

这是在文本框更改事件中调用的控制器代码:

$scope.doAutoCompleteType = function() {
    $scope.suggestedItems.length = 0;
    var text = $scope.searchText;
    console.log("=========>"+text);
}

这里是html代码:

<input type="text" placeholder="Type text.." ng-model="searchText" ng-change="doAutoCompleteType()">

问题是console.log("=========>"+text);正在打印undefined

3 个答案:

答案 0 :(得分:1)

$scope.doAutoCompleteType = function(text) {
    $scope.suggestedItems.length = 0;
    console.log("=========>"+text);
}


<input type="text" placeholder="Type text.." ng-model="searchText" ng-change="doAutoCompleteType(searchText)">

答案 1 :(得分:1)

我没有发现您的代码有任何问题。你能发布更多代码吗?以下是ng-change的示例:

<body ng-app="changeExample">
 <script>
  angular.module('changeExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.change = function() {
        $scope.text2 = $scope.text;
        var print = $scope.text;
        console.log("***" + print);
      };
    }]);
</script>
<div ng-controller="ExampleController">
  <input type="text" ng-model="text" ng-change="change()" id="ng-change-example1" /><br/>
  <tt>text2 = {{text2}}</tt><br/>
</div>
</body>

http://plnkr.co/edit/LVQqCIBW3kGpN4bIzVwK?p=preview

答案 2 :(得分:0)

它应该有一些拼写错误或父子范围问题,在下面的链接中被泄露

angularjs scope(the crux): parent child scope having model(with and without 'dot')