如何将struts1与angularjs混合以自动填充字段

How to mix struts1 with angularjs to auto populate a field

本文关键字:填充 字段 混合 angularjs struts1      更新时间:2023-09-26

我正在用struts1构建一个jsp页面(我不能使用struts2),其中我有一个字段,我正在用表单中的值自动填充,但用户可以自由覆盖该文本字段中的任何内容。我还使用了AngularJS,以便在用户重写struts预先填充的值时,在用户键入(类似谷歌的搜索)时提供选项。

我的问题是struts中的html:text标记无法识别标记ng-model和uib-typeahead。如果我切换到纯html输入标记,那么angular特性就可以工作,但现在我无法用struts预先填充字段。

script.js:

  angular.module('plunker', ['ui.bootstrap','ngAnimate']).controller('TypeaheadCtrl', function($scope, $http) {
  $scope.selected = undefined;
  // Any function returning a promise object can be used to load values asynchronously
  $scope.getLocation = function(val) {
    return $http.get('http://maps.googleapis.com/maps/api/geocode/json', {
      params: {
        address: val,
        sensor: false
      }
    }
  ).then(function(response){
        return response.data.results.map(function(item){
          return item.formatted_address;
        });
      });
    };
  });

mypage.html

<body ng-controller="TypeaheadCtrl">
 <div class='container-fluid typeahead-demo' >
   <div class="section">
         <!-- OPTION 1: angular works and gives suggestions, but the field doesn't get pre-populated by struts-->
         <input type="text" name="city" id="city" class="gui-input" ng-model="asyncSelected" uib-typeahead="address for getLocation($viewValue)"  placeholder="Type city name">
         <html:hidden property="city"/>
         <!-- OPTION 2: auto-populates value but it can't compile with ng-model and typegead tags -->
         <html:text property="city" styleClass="gui-input" styleId="cityStyle" />
   </div>
</div>

有人能告诉我最好的方法或合适的方法是什么吗?如何混合选项1和选项2的功能?

提前谢谢。

我已经通过在加载时使用jquery设置字段的值来解决了这个问题,但如果有人有更好的解决方案或更好的实践,我想知道。