0

I have the following form in my Angular application:

<form id="form1">
    <fieldset>
       <input type="text" name="field1" ng-model="frm.myID" />
       <input type="button" name="btnProcess" class="action-button" value="Process" ng-click="Process()" />
   </fieldset>
</form>

In my controller I have:

var frm = this;

$scope.Process = function() {
   console.log(frm.myID);

}

frm.myID comes up as undefined. What am I missing?

3
  • please provide the entire controller definition and HTML block, including where ng-controller is declared. Commented Nov 11, 2016 at 16:07
  • this works correctly, with the limited information you provided, as long as one fills in the blanks and makes some assumptions about the rest of the missing code: plnkr.co/edit/71Y2Tzj0hgmWg49g1vH6?p=preview. Something you haven't posted in the question body is not configured properly, and the rest of the code is necessary in order to identify the issue. Commented Nov 11, 2016 at 16:12
  • as a side note, if you are using this, you generally shouldn't be saving values on $scope. and if you are using $scope, then the var frm = this is unnecessary. Using both syntaxes at the same time only leads to confusion. Commented Nov 11, 2016 at 16:14

3 Answers 3

1

You're assigning controller function to frm variable in controller code, so the correct console.log should be console.log(frm.frm.myId). Of course, if you're not using controller as syntax.

Update. You probably don't need to frm to this. So try to delete this var frm = this. And it will work with console.log console.log($scope.frm.id)

Sign up to request clarification or add additional context in comments.

2 Comments

that would only be true if frm == $scope, which isn't demonstrated here either.
Yes, good point. I guess then just there is no need in var frm = this.
0

You're not using the object defined on the $scope. Check the below code snippets.

Inside your controller, you seem to be mixing the coding styles i.e. $scope syntax vs. controller aliasing syntax, please go with one approach.

Also please initialize your object to an empty Object i.e. $scope.frm = {};

$scope syntax.

angular
  .module('demo', [])
  .controller('DefaultController', DefaultController);

DefaultController.$inject = ['$scope'];

function DefaultController($scope) {
  $scope.frm = {};

  $scope.Process = function() {
    console.log($scope.frm.myID);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="demo">
  <div ng-controller="DefaultController">
    <form id="form1">
      <fieldset>
        <input type="text" name="field1" ng-model="frm.myID" />
        <input type="button" name="btnProcess" class="action-button" value="Process" ng-click="Process()" />
      </fieldset>
    </form>
  </div>
</div>

controller aliasing syntax. You can use $scope in this syntax too but for doing things like setting up watchers, events, etc.

angular
  .module('demo', [])
  .controller('DefaultController', DefaultController);

function DefaultController() {
  var vm = this;
  vm.frm = {};
  vm.process = process;

  function process() {
    console.log(vm.frm.myID);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="demo">
  <div ng-controller="DefaultController as ctrl">
    <form id="form1">
      <fieldset>
        <input type="text" name="field1" ng-model="ctrl.frm.myID" />
        <input type="button" name="btnProcess" class="action-button" value="Process" ng-click="ctrl.process()" />
      </fieldset>
    </form>
  </div>
</div>

2 Comments

I still get $scope.frm.myID as undefined... Could it be because the page is a partial page, loaded withing another page?
Seems to me that you are using controller as syntax, Could you please try as the e.g. code snippet I've given with controller aliasing syntax, initially it will print undefined just type some text and click Process button and check
0

myapp= angular.module("app",[])
       myapp.controller("yourControllerName",function($scope){
    var frm = this;
    this.myID = ""; // ur missing
    this.Process = function() {
      
       console.log(frm.myID);

    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"></script>
<div ng-app="app" ng-controller="yourControllerName as frm">
<form id="form1">
    <fieldset>
       <input type="text" name="field1" ng-model="frm.myID" />
     
       <input type="button" name="btnProcess" class="action-button" value="Process" ng-click="frm.Process()" />
   </fieldset>
</form>
</div>

1 Comment

I added frm.myID=""; before my function and I already had my controller with as syntax in my config file, but now console.log(frm.myID); comes up as blank

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.