如何使用形式自定义模板初始化传单地图?

时间:2016-07-07 19:19:40

标签: javascript angularjs leaflet angular-formly

我正在使用Angular-Formly创建一个表单,您可以在其中输入一个地址并从该本地返回一个地图。我创建了一个包含以下内容的自定义模板:

>>>>>> ------Exception------- >>>>>> Class: Kitchen::ActionFailed >>>>>> Message: 1 actions failed. >>>>>> Failed to complete #create action: [unexpected token 0] on apache-centos-72 >>>>>> ---------------------- >>>>>> Please see .kitchen/logs/kitchen.log for more details >>>>>> Also try running `kitchen diagnose --all` for configuration D ------Exception------- D Class: Kitchen::ActionFailed D Message: 1 actions failed. >>>>>> Failed to complete #create action: [unexpected token 0] on apache-centos-72 D ---------------------- D ------Backtrace------- D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/command.rb:187:in `report_errors' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/command.rb:178:in `run_action' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/command/action.rb:39:in `block in call' D /usr/share/ruby/benchmark.rb:281:in `measure' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/command/action.rb:37:in `call' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/cli.rb:56:in `perform' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/cli.rb:184:in `block (2 levels) in <class:CLI>' D /usr/local/share/gems/gems/thor-0.19.1/lib/thor/command.rb:27:in `run' D /usr/local/share/gems/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/cli.rb:325:in `invoke_task' D /usr/local/share/gems/gems/thor-0.19.1/lib/thor.rb:359:in `dispatch' D /usr/local/share/gems/gems/thor-0.19.1/lib/thor/base.rb:440:in `start' D /usr/local/share/gems/gems/test-kitchen-1.10.2/bin/kitchen:13:in `block in <top (required)>' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/errors.rb:174:in `with_friendly_errors' D /usr/local/share/gems/gems/test-kitchen-1.10.2/bin/kitchen:13:in `<top (required)>' D /usr/local/bin/kitchen:23:in `load' D /usr/local/bin/kitchen:23:in `<main>' D ----End Backtrace----- D -Composite Exception-- D Class: Kitchen::ActionFailed D Message: Failed to complete #create action: [unexpected token 0] on apache-centos-72 D ---------------------- D ------Backtrace------- D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/parser.rb:50:in `method_missing' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/parser.rb:61:in `expr' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/parser.rb:161:in `led_comparator' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/parser.rb:63:in `expr' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/parser.rb:39:in `parse' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/caching_parser.rb:25:in `block in cache_expression' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/caching_parser.rb:23:in `synchronize' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/caching_parser.rb:23:in `cache_expression' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/caching_parser.rb:16:in `parse' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/runtime.rb:56:in `search' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath.rb:47:in `search' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/poller.rb:70:in `matches_path?' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/poller.rb:65:in `acceptor_matches?' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/poller.rb:49:in `block in call' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/poller.rb:48:in `each' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/poller.rb:48:in `call' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/waiter.rb:104:in `block in poll' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/waiter.rb:101:in `loop' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/waiter.rb:101:in `poll' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/waiter.rb:91:in `block (2 levels) in wait' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/waiter.rb:90:in `catch' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/waiter.rb:90:in `block in wait' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/waiter.rb:89:in `catch' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/waiter.rb:89:in `wait' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/client_waiters.rb:110:in `wait_until' D /usr/local/share/gems/gems/aws-sdk-resources-2.3.20/lib/aws-sdk-resources/operations.rb:229:in `call' D /usr/local/share/gems/gems/aws-sdk-resources-2.3.20/lib/aws-sdk-resources/operation_methods.rb:19:in `block in add_operation' D /usr/local/share/gems/gems/kitchen-ec2-1.0.0/lib/kitchen/driver/ec2.rb:173:in `create' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:449:in `public_send' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:449:in `block in perform_action' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:513:in `call' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:513:in `synchronize_or_call' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:478:in `block in action' D /usr/share/ruby/benchmark.rb:281:in `measure' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:477:in `action' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:449:in `perform_action' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:359:in `create_action' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:348:in `block in transition_to' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:347:in `each' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:347:in `transition_to' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:127:in `create' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/command.rb:201:in `public_send' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/command.rb:201:in `run_action_in_thread' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/command.rb:173:in `block (2 levels) in run_action' D ----End Backtrace----- D ---Nested Exception--- D Class: Kitchen::ActionFailed D Message: Failed to complete #create action: [unexpected token 0] D ---------------------- D ------Backtrace------- D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/parser.rb:50:in `method_missing' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/parser.rb:61:in `expr' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/parser.rb:161:in `led_comparator' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/parser.rb:63:in `expr' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/parser.rb:39:in `parse' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/caching_parser.rb:25:in `block in cache_expression' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/caching_parser.rb:23:in `synchronize' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/caching_parser.rb:23:in `cache_expression' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/caching_parser.rb:16:in `parse' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath/runtime.rb:56:in `search' D /usr/local/share/gems/gems/jmespath-1.2.4/lib/jmespath.rb:47:in `search' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/poller.rb:70:in `matches_path?' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/poller.rb:65:in `acceptor_matches?' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/poller.rb:49:in `block in call' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/poller.rb:48:in `each' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/poller.rb:48:in `call' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/waiter.rb:104:in `block in poll' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/waiter.rb:101:in `loop' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/waiter.rb:101:in `poll' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/waiter.rb:91:in `block (2 levels) in wait' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/waiter.rb:90:in `catch' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/waiter.rb:90:in `block in wait' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/waiter.rb:89:in `catch' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/waiters/waiter.rb:89:in `wait' D /usr/local/share/gems/gems/aws-sdk-core-2.3.20/lib/aws-sdk-core/client_waiters.rb:110:in `wait_until' D /usr/local/share/gems/gems/aws-sdk-resources-2.3.20/lib/aws-sdk-resources/operations.rb:229:in `call' D /usr/local/share/gems/gems/aws-sdk-resources-2.3.20/lib/aws-sdk-resources/operation_methods.rb:19:in `block in add_operation' D /usr/local/share/gems/gems/kitchen-ec2-1.0.0/lib/kitchen/driver/ec2.rb:173:in `create' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:449:in `public_send' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:449:in `block in perform_action' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:513:in `call' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:513:in `synchronize_or_call' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:478:in `block in action' D /usr/share/ruby/benchmark.rb:281:in `measure' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:477:in `action' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:449:in `perform_action' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:359:in `create_action' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:348:in `block in transition_to' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:347:in `each' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:347:in `transition_to' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/instance.rb:127:in `create' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/command.rb:201:in `public_send' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/command.rb:201:in `run_action_in_thread' D /usr/local/share/gems/gems/test-kitchen-1.10.2/lib/kitchen/command.rb:173:in `block (2 levels) in run_action' D ----End Backtrace-----

并将类型设置为<script type="text/ng-template" id="leaflet-map-template.html"> <div id="[options.key]"></div> </script>与其他模块:

angular.module

但在现场我不知道如何初始化传单地图。这是我的字段数组的一部分:

var app = angular.module('formlyExample', [
    'ngAnimate',
    'ngSanitize',
    'ngTouch',
    'formly',
    'formlyBootstrap',
    'ui.bootstrap'
  ], function config(formlyConfigProvider) {
    // set custom template here
    formlyConfigProvider.setType({
      name: 'leaflet-map',
      templateUrl: 'leaflet-map-template.html'
    });
  });

- EDIT-- 它给我的错误是:vm.formFields = [ //other fields come here //leaflet map template { key: 'mymap', type: 'leaflet-map', templateOptions: { label: 'Leaflet Map' }, controller: /* @ngInject */ function($scope) { var initialCoordinates = [-23.0895164, -47.2187371]; // initialize map with initial coordinates var map = L.map($scope.options.key, { center: initialCoordinates, zoom: 14, zoomControl: false }); } }]; ,因为找不到Map container not found

1 个答案:

答案 0 :(得分:3)

这不起作用,因为控制器将在DOM中没有具有正确ID的元素的位置被调用,因为Formly尚未应用模板。

那么......如何解决?首先,您应该使用link函数而不是controller,因为链接函数是在Angular中进行DOM操作的默认位置。

此外,为了避免每次实例化地图字段时都必须提供链接功能,请将链接功能放在类型定义中,而不是字段定义中。

最后,链接函数接收封闭元素作为第二个参数,因此获取div所需要做的只是获取封闭元素的第一个子元素。

代码如下所示:

formlyConfigProvider.setType({
  name: 'leafletmap',
  templateUrl: 'leaflet-map-template.html',
  link: function(scope, el) {
    // initialize map with initial coordinates
    var initialCoordinates = [-23.0895164, -47.2187371];
    // get first child of the enclosing element - the <div>!
    var mapDiv = el.children()[0];
    var map = L.map(mapDiv, {
      center: initialCoordinates,
      zoom: 14,
      zoomControl: false
    });
  }
});

哦,我忘了告诉你两件事:

首先,如果你想将字段键作为ID传递,你应该像普通的角度模板一样,使用双花括号:

<script type="text/ng-template" id="leaflet-map-template.html">
  <div id="{{options.key}}"></div>
</script>

最后,您不需要在div中添加ID,因为我们正在使用封闭字段元素的第一个子元素来选择它。 :)

为了把它包起来,我在codepen上放了一个最小的工作示例,看看: https://codepen.io/sigriston/pen/OXxPPb