【问题标题】:Dynamic HTML partial based on REST response基于 REST 响应的动态 HTML 部分
【发布时间】:2017-04-09 19:21:34
【问题描述】:

我正在使用 Angular JS 前端和为 MySQL 数据库提供基于 REST 的 API 后端构建一个应用程序。有从前端到后端的 REST 调用来填充或检索数据库中的数据。我想在我的 Angular JS 前端主页中添加一个下拉选择框。我希望选择触发对数据库的 REST 调用,以检索特定值并使该值成为动态加载的 html 部分的一部分。

例如,下拉菜单将选择汽车的型号(Toyota Corolla、Honda Accord 等)。当您选择该型号时,控制器将对相应的表进行 REST 调用以获取该车的其余信息(MPG、尺寸、重量等)一旦这样做,它将在页面上加载部分 HTML,该页面是模板 HTML 文件,但具有动态内容。所以加载的页面是/#/carInfo?toyotaCorolla。模板部分 html 文件将加载,然后模板上的表将填充来自该 REST 调用的响应。因此,我基本上会为该页面提供一个模板,但它会根据所选内容调用页面的新版本。

我脑子里正在考虑这个问题,但我没有随身携带我的应用程序代码。这个问题不是针对实际的代码解决方案,而是针对某人编写一些伪代码或将我指向与此类似的在线演示/示例……如果可能的话。我正在自己进行搜索,但我可能正在搜索错误的术语来完成这项工作。对此的任何指示或帮助将不胜感激。

更新: 现在我回家了,这是我遇到问题的代码的 sn-p。

<ul class="nav navbar-nav">
    <li><a href="#/home" class="mdi-action-home"></a></li>
    <li class="dropdown">
        <a href="javascript:void(0)" data-target="#" class="dropdown-toggle" data-toggle="dropdown">
            Select a car...
            <b class="caret"></b></a>
        <ul class="dropdown-menu">
            <li ng-model="selectedCar.value" ng-repeat="x.car for x in cars"
                ng-change="selectedCarChanged()"></li>
        </ul>
    </li>
</ul>

没有正确填充。对于使用 ng-options 而不是 ng-repeat 的 &lt;select&gt; 实现,我有相同的 ng 代码。我希望这是一个简单的过渡,但使用列表的 CSS 版本不起作用。

【问题讨论】:

  • “我希望选择触发对数据库的 REST 调用,以检索特定值并使该值成为动态加载的 html 部分的一部分。” 看起来就像您正在寻找的是使用AJAX。 Angular 使用 $http 进行 AJAX 调用。

标签: javascript angularjs rest partials


【解决方案1】:

请在下面找到代码 sn-p。希望这会有所帮助

汽车列表.html

<div ng-controller="carListController">
    <select ng-model="selectedCar" ng-change="onSelectCar(selectedCar)">
        <option ng-repeat="car in cars">{{car}}</option>
    </select>
</div>

carListController.js

app.controller('carListController', function($scope, $location) {
    $scope.carList = ['Honda', 'Toyota', 'Suzuki', 'Hyundai'];
    $scope.onSelectCar = function(car) {
        $location.path('#/carInfo').search({carInfo: car});
    }
});

carInfo.html

<div class="carDetails">
    <span>Car Name: {{car.name}}</span>
    <span>Car Model: {{car.model}}</span>
    <span>Car Year: {{car.year}}</span>
    <span>Car Size: {{car.size}}</span>
</div>

carInfoDetailsController.js

app.controller('carInfoController', function($scope, $location, $http) {
    $scope.car = {};
    $scope.init= function() {
        $http.get('url/' + $location.search('carInfo'), function(response) {
              $scope.car = response;
        });
    };

    $scope.init();
});

appConfig.js

app.config(function($routeProvider){
     $routeProvider.when('/carInfo'{
         templateUrl: "carInfo.html",
         controller: "carInfoController"
     });
});

【讨论】:

  • 这绝对是我需要的。唯一让我失望的是我没有使用标准的
【解决方案2】:

类似:

  //in a service    
  (function() {
    function MyService($http) {
        var myService = {};
        MyService.accessMultiTool = function(){
          var args = Array.from(arguments);
          var method, url, authorization;
          args.forEach(function(item){
            if('method' in item){
                    method = item.method;
                }else if ('url' in item){
                    url = item.url;
                }else if ('authorization' in item){
                    authorization = item.authorization;
            }
          });
        delete $http.defaults.headers.common['X-Requested-With'];
        return $http({
                      method: method,
                      origin: 'http://someclient/',
                      url: url,
                      headers: {'Authorization': authorization}
               }).error(function(status){generate some error msg});
       };

    return MyService;
    }

    angular
        .module('myApp')
        .factory('MyService', ['$http', MyService]);
  })();

  //in a controller
  (function () {
      function MyCtrl(MyService) {
          var myController = this;
          this.car_model_options = ["Honda", "Chevy", "Ford", "Nissan"];
          this.bound_car_model_obj = {
             model: null
          };
          this.getCarModel = function(){
            MyService.accessMultiTool({method: 'GET'}, {url: 'http://somebackend/api/cars/' + myController.bound_car_model_obj.model}, {authorization: this.activeMember.auth}).then(function(data){
            myController.setCurrCarModel(data);
          });
          this.setCurrCarModel = function(data){
            myController.currently_selected_car_model = data;
          };
      };

    };

  angular
    .module('myApp')
    .controller('MyCtrl', ['MyService', MyCtrl]);
  })();

  //in a template
  <div ng-controller="MyCtrl as mycontroller">
    <select data-ng-init="this.bound_car_model_obj.model = mycontroller.car_model_options[0]" data-ng-model="this.bound_car_model_obj.model" data-ng-options="option for option in mycontroller.car_model_options" >
    </select>

    <table>
      <tr ng-repeat="car in mycontroller.currently_selected_car_model>
        <td>{{car.someproperty}}>/td> 
        <td>{{car.someotherproperty}}>/td>
      </tr>
    </table>
  </div>

【讨论】:

    猜你喜欢
    • 2015-03-24
    • 2016-08-10
    • 2016-09-24
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    相关资源
    最近更新 更多