5

I am wondering at the dual behaviour of $scope. In the below script I am getting value of name as alert. But in my ionic app the same code alerts undefined.

I googled the problem and found this link as a solution where it states that we need to use dot(.) in order to get the value in ng-model. What is the difference between two.

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.a =function a(){alert($scope.name);}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
Name: <input ng-model="name" ng-blur="a()">
</div>
sam
  • 688
  • 1
  • 12
  • 35

4 Answers4

0

Try changing your controller function as below:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.a =function(){
        alert($scope.name);
    }
});
daan.desmedt
  • 3,752
  • 1
  • 19
  • 33
0

Actually it does work with Ionic,

angular.module('starter.controllers', [])
.controller('myCtrl', function($scope) {
  $scope.a = function a() {
    alert($scope.name);
  }

})

DEMO

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

Solution :

"If you use ng-model, you have to have a dot in there."
Make your model point to an object.property and you'll be good to go.

Controller

$scope.formData = {};
$scope.check = function () {
  console.log($scope.formData.searchText.$modelValue); //works
}

Template

<input ng-model="formData.searchText"/>
<button ng-click="check()">Check!</button>

This happens when child scopes are in play - like child routes or ng-repeats. The child-scope creates it's own value and a name conflict is born as illustrated here:

See this video clip for more: https://www.youtube.com/watch?v=SBwoFkRjZvE&t=3m15s .

And that is referred from below links :

Other Solutions

Use this keyword instead of $scope, More details



And also you can get more details from this below two discussions

Ng-model does not update controller value

Why is my ng-model variable undefined in controller?


Update Solution 1 :

Please declaring the blank object first at the top of your controller:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.name = "";
    $scope.a = function(){alert($scope.name);}
});

I hope these will be helps to you.

Community
  • 1
  • 1
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • But in the above snippet you can see that I am not using 'dot' with ng-model but it still gives the correct output – sam Jan 30 '17 at 11:02
  • I think it may be threw the problem for `$parent` and `$child`manipulation. let me know if my all solution is not works to you – Ramesh Rajendran Jan 30 '17 at 11:07
0

Try to use json object.

  var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope) {
        $scope.user = {'name':''};
        $scope.a =function a(){alert($scope.user.name);}
    });

 <div ng-app="myApp" ng-controller="myCtrl">
Name: <input ng-model="user.name" ng-blur="a()">
</div>
IftekharDani
  • 3,619
  • 1
  • 16
  • 21