2017-02-20 2 views
0

Ich arbeite mit JSP und Angular JS. Ich habe eine JSP-Seite mit einem versteckten Eingabefeld. Ein Sitzungsattribut wird wie folgt auf seinen Wert gesetzt.Bind-Wert eines Eingangs in Controller-AngularJS

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

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

Wie kann ich binden den Wert des Eingabefeldes auf eine Variable $ scope.policy in meinem Controller.

JS

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

Shareer, es funktioniert? –

+0

[Ihre Frage und meine Antwort] (http://stackoverflow.com/questions/42300219/accessing-jsp-variable-from-javascript-angular-js/42302283#42302283) ist es nicht funktioniert? –

Antwort

0

Sie können für Ihre input diese Einstellung ng-model Richtlinie tun:

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

In der Steuerung Sie watch Methode verwenden.

$ Uhr hilft für $ scope zu hören ändert

JS

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

Einfaches Beispiel:

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>

Hinweis: Normalerweise watch Methode ist es sehr hilfreich, wenn Sie some Code ausführen möchten, wenn die $scope.variable es geändert wird.

Verwandte Themen