Ionic Framework controller (AngularJS) cannot see declared ng model

This is my HTML code.

<ion-view view-title="Home" ng-controller="makeOrderController"> <input type="search" placeholder="Tìm kiếm" ng-model="searchKeyword" ng-change="searchMenu()" > </ion-view> 

This is my js code

 .... .controller('makeOrderController', ['$scope', function ($scope) { $scope.searchMenu = function ($scope) { console.log($scope.searchKeyword); } }]); 

Yes. When I type a search text field, the searchMenu () method is executed, but it causes an error

 Cannot read property 'searchKeyword' of undefined 

I searched for SO, I tried:

  • To add $parent. in ng-model

  • Or use the js operator - $ scope.model.searchKeyword . But this code does not work :(

Please help me help console.log write the updated keyword by user type.

+4
source share
1 answer

You have 2 $scope variables. Thus, the largest $scope takes preference undefined.

Get rid of it.

 .controller('makeOrderController', ['$scope', function ($scope) { $scope.searchMenu = function () { console.log($scope.searchKeyword); } }]); 

Fiddle

+6
source

All Articles