0

I am working with JSP and Angular JS. I have a JSP page with a hidden input field. a session attribute is set to its value as follows.

  String policy = (String)session.getAttribute("POLICY_CHANGE");

  <input type="hidden" value="<%=policy%>" name="policy" ng-model="$scope.policyChange" />

How can i bind the value of the input field to a variable $scope.policy in my controller.

JS

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
   $scope.policyChange= ; // i want to bind the input field value here.
});
1

1 Answer 1

0

You can do this setting ng-model directive for your input:

 <input type="hidden" value="<%=policy%>" name="ng2_session" ng-modal="vm.policyChange" ng-model="policy" ng-init="policy='<%=policy%>'" />

In the controller you have to use watch method.

$watch helps to listen for $scope changes

JS

 app.controller('myCtrl', function($scope) {
   console.log($scope.policy); // i want to bind the input field value here.
 });

Simple example:

function MyCtrl($scope) {
   $scope.$watch('policy', function() {
      console.log($scope.policy);
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
  <h2>Todo</h2>
  <div ng-controller="MyCtrl">
      <input type="text" ng-model="policy" ng-init="policy='Bob'"/>
  </div>
</div>

Note: Usually, watch method it is very helpful when you want to run some code when the $scope.variable it is changed.

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

Comments

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.