0

I'm trying to read a $scope variable value inside my angular controller function, but I got the undefined, when I tried to print it in the console, which is pretty strange since I can successfully set its value inside the controller and show it correctly in my html, here is the controller part I tried to read this value:

$scope.findUserByTelephone = function() {
    console.log($scope.telephone);
}

my html:

      <label for="telephone">telephone:</label>
      <input type="number" id="telephone" ng-model="telephone"/>   
      <button ng-click="findUserBytelephone()" >search</button>

When I click the button, I got undefined print out. But if I set $scope.telephone value inside the controller before the findUserByTelephone() function :

    $scope.telephone = 1211;

This value can be print out, and it can be showed in my html input area. But if I change this value in the input area, it still print out the original value I set in the controller. In this case, still 1211. Another thing is if I put

{{telephone}}

in my html, this area does change with my input, but the value print out inside the controller is still the old value. Is there anyone can tell me where goes wrong and how can I fix this? Thx in advance.

For those who want to check the entire project, here is the link: https://github.com/pousT/Voluncare

I am new to angular and is still learning during the development, thanks for all helps!

pousT
  • 23
  • 6
  • What if you declare the scope variable as an empty string in your controller? $scope.telephone = ""; – Sealer_05 Feb 23 '17 at 00:42
  • 1
    Did you inject $scope in controller function? – MukulSharma Feb 23 '17 at 00:44
  • @Mark_1955 I can't in this case since i set its type to number, another thing is if I use {{telephone}} in the html, the value does change as I change my input. – pousT Feb 23 '17 at 00:47
  • try declare your telephone as object $scope.telephone = {text: ""}, and ng-model="telephone.text". Another way to fix may be to pass the telephone to the function call findUserBytelephone(telephone) – Charlie Ng Feb 23 '17 at 00:49
  • @CharlieNg I tried the first solution and it works, thank you so much! But still I can't figured out why this happens, and I have tried the second way before and passed telephone by `ng-click = "findUserBytelephone({{telephone}})"`, and I got undefined in my controller as well. – pousT Feb 23 '17 at 00:56
  • 1
    `findUserByTelephone` not `findUserBytelephone` – Ronnie Feb 23 '17 at 00:57
  • @Ronnie ohh, don't worry about that, I spelled right in my code. – pousT Feb 23 '17 at 01:00
  • 1
    ok, well not sure what to tell you without seeing all your code. What you are saying works just fine https://jsfiddle.net/ojzdxpt1/11/ I literally copied your code – Ronnie Feb 23 '17 at 01:01
  • It's likely that you are a victim of [JavaScript Prototype Inheritance](http://stackoverflow.com/questions/14049480/what-are-the-nuances-of-scope-prototypal-prototypical-inheritance-in-angularjs?rq=1). It is difficult to know for sure without seeing your *entire* code, but you are breaking one of the **cardinal rules** of angular here: **always use a dot in angular bindings**. – Claies Feb 23 '17 at 01:04
  • yup. Read on the link that Claies suggested. Also, the second way should work. please do `ng-click="findUserByTelephone(telephone)"` and in the controller define the function like `$scope.findUserByTelephone = function(text) { console.log(text); }` – Charlie Ng Feb 23 '17 at 01:22
  • @Claies Thanks for your advice, I have put the project link in my problem description, if you are interested, please have a look:) the problem occurs at app_client/records/ – pousT Feb 23 '17 at 01:23
  • just FYI you'll be throwing errors of `message is undefined` if you end up with an error from your API currently – haxxxton Feb 23 '17 at 01:33
  • Also, if you already have all the `records` which include the key `telephone` you do not need to call a second API to "search". Angularjs has a filter for being able to "search" your repeat. [Have a look here](http://stackoverflow.com/questions/14733136/ng-repeat-filter-by-single-field) – haxxxton Feb 23 '17 at 01:35
  • @haxxxton not sure which part you are talking about, but record and user are from different schemas, so there is no duplicated search – pousT Feb 23 '17 at 01:56
  • right, apologies, i assumed the records contained user data. As for the `undefined` issue i mentioned.Lines 9, 13, and 31 of your controller.. unless you're using a **shudder** _global_ – haxxxton Feb 23 '17 at 01:58
  • 1
    @haxxxton oh yes you are right, I did not declare and use this `message` variable in my html, thx for pointing that out. – pousT Feb 23 '17 at 02:16
  • Do you write your HTML controls shown in question inside ng-repeat or ng-if block ? – Ankit Vadi Feb 23 '17 at 05:05

0 Answers0