UnworthyToast UnworthyToast - 1 year ago 71
AngularJS Question

Accessing ngModel elements that use a class

I have many elements in my HTML code that have their

assignment defined as
ng-model = "object.[something]"

For example:

<div class="form-group row" ng-model="object.askUser">

I do this to be clear of my purpose for these elements. My question is how do I access these element in my Javascript? Do I call
, or something else? I had a hard time finding things on the web about this, most likely because I wasn't quite sure of the words to use in the search bar to describe what I am trying to do.

Answer Source

Inside your controller use $scope.object.askUser:

var app = angular.module('TestApp', []);

app.controller("testCtrl", function ($scope) {
  $scope.someObject = {};
  $scope.someObject.askUser = "Hello, world!";
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="TestApp">
  <div ng-controller="testCtrl">
    <input ng-model="someObject.askUser" />

Side note:

You use in your example <div> with ngModel.

ngModel Docs:

The ngModel directive binds an input,select, textarea (or custom form control) to a property on the scope

If you want to one-way bind a model to a div use Angular Expression:

<div class="form-group row">
   {{ object.askUser }}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download