【问题标题】:AngularJS $stateProvider URL results in blank pageAngularJS $stateProvider URL 导致空白页面
【发布时间】:2016-10-08 13:26:52
【问题描述】:

我正在使用 angular-ui-router 并设置了一个类似于我的第一个控制器的第二个控制器,它正在工作,但是第二个控制器中的路由没有正确路由应用程序。谁能告诉我哪里出错了?

第一个控制器的html(正常工作):

<div style="height: 100%"> <!--took out: ng-if="map.center !== undefined"-->
    <ui-gmap-google-map 
                        center='map.center'
                        zoom='map.zoom'
                        draggable='map.draggable'
                        dragging='map.dragging'
                        refresh='map.refresh'
                        options='map.options'
                        events='map.events'
                        pan='map.pan'>


        <ui-gmap-circle 
                        center='map.circle.center'
                        radius='map.circle.radius'
                        fill='map.circle.fill'
                        stroke='map.circle.stroke'
                        clickable='map.circle.clickable'
                        draggable='map.circle.draggable'
                        editable='map.circle.editable'
                        visible='map.circle.visible'
                        events='map.circle.events'>

        </ui-gmap-circle>


    </ui-gmap-google-map>
<script src='//maps.googleapis.com/maps/api/js?key=AIzaSyD_g7xCYEi-U54SYfTXQ_lukRsChkWgjXQ'></script>
</div>

第一个控制器(正常工作):

(function (window, ng) {
    ng.module('app', ['uiGmapgoogle-maps', 'ui.router'])



  .config(function ($stateProvider) { //had: , $stateChangeError included in the function parameters, but that caused error 
      $stateProvider.state('searchRadius', {
          url: '/:lat/:lon',
          templateUrl: 'searchRadius.html', //changed from  index to searchRadius.html
          controller: 'MapsCtrl',
      });
  })


    .controller('MapsCtrl', ['$scope', "uiGmapLogger", "uiGmapGoogleMapApi", "$interval", "$state", "$stateParams",
      function ($scope, $log, GoogleMapApi, $interval, $state, $stateParams) {
          $log.currentLevel = $log.LEVELS.debug;
          var center = { latitude: parseFloat($stateParams.lat), longitude: parseFloat($stateParams.lon) };
          alert(JSON.stringify(center));
          Object.freeze(center); //caused TypeError: Cannot assign to read only property ('latitude') ...

          console.log($stateParams);

          $scope.map = {
              center: center,
              pan: false,
              zoom: 16,
              refresh: false,
              events: {},
              bounds: {}
          };

          $scope.map.circle = {
              id: 1,
              center: center,
              radius: 500, //(current time - date lost)*km/hour
              stroke: {
                  color: '#08B21F',
                  weight: 2,
                  opacity: 1
              },

              fill: {
                  color: '#08B21F',
                  opacity: 0.5
              },
              geodesic: false, // optional: defaults to false
              draggable: false, // optional: defaults to false
              clickable: true, // optional: defaults to true
              editable: false, // optional: defaults to false
              visible: true, // optional: defaults to true
              events: {
                  dblclick: function () {
                      $log.debug("circle dblclick");
                  },
                  radius_changed: function (gObject) {
                      var radius = gObject.getRadius();
                      $log.debug("circle radius radius_changed " + radius);
                  }
              }
          }

          //Increase Radius:
          $interval(function(){
                $scope.map.circle.radius += 30; //dynamic var
          }, 1000); //end of interval function


      } ]); //end of controller

})(window, angular);

第二个 html(空白页):

<!--Add ability to input location as address-->

<div>
        <ui-gmap-google-map 
                        center='map.center'
                        zoom='map.zoom'
                        draggable='map.draggable'
                        dragging='map.dragging'
                        refresh='map.refresh'
                        options='map.options'
                        events='map.events'
                        pan='map.pan'>


        <ui-gmap-circle 
                        center='map.circle.center'
                        radius='map.circle.radius'
                        fill='map.circle.fill'
                        stroke='map.circle.stroke'
                        clickable='map.circle.clickable'
                        draggable='map.circle.draggable'
                        editable='map.circle.editable'
                        visible='map.circle.visible'
                        events='map.circle.events'>

        </ui-gmap-circle>


    </ui-gmap-google-map>

    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDWKJRXSux3dAdEYOYqjkoi2MCW8dutFbY&callback=initMap">
    </script>
</div>

第二个控制器(导致空白页没有错误):

(function (window, ng) {
    ng.module('app', ['uiGmapgoogle-maps', 'ui.router'])



  .config(function ($stateProvider) { //had: , $stateChangeError included in the function parameters, but that caused error 
      $stateProvider.state('getLocation', {
          url: '/getLocation',
          templateUrl: 'getlocation.html', //changed from  index to searchRadius.html
          controller: 'GetlocationCtrl',
      });
  })



    .controller('GetlocationCtrl', ['$scope', "uiGmapLogger", "uiGmapGoogleMapApi", "$state", "$stateParams",
      function ($scope, $log, GoogleMapApi, $state, $stateParams) {
          $log.currentLevel = $log.LEVELS.debug;
          var center = { latitude: 49.22, longitude: -122.66 }; //default center
          alert(JSON.stringify(center));

          console.log($stateParams);

          $scope.map = {
              center: center,
              pan: false,
              zoom: 16,
              refresh: false,
              events: {},
              bounds: {}
          };

          $scope.map.circle = {
              id: 1,
              center: center,
              radius: 500, //(current time - date lost)*km/hour
              stroke: {
                  color: '#08B21F',
                  weight: 2,
                  opacity: 1
              },

              fill: {
                  color: '#08B21F',
                  opacity: 0.5
              },
              geodesic: false, // optional: defaults to false
              draggable: false, // optional: defaults to false
              clickable: true, // optional: defaults to true
              editable: false, // optional: defaults to false
              visible: true, // optional: defaults to true
              events: {
                  dblclick: function () {
                      $log.debug("circle dblclick");
                  },
                  radius_changed: function (gObject) {
                      var radius = gObject.getRadius();
                      $log.debug("circle radius radius_changed " + radius);
                  }
              }
          }


      } ]); //end of controller

})(window, angular);

【问题讨论】:

  • 您是否尝试声明该模块两次?

标签: angularjs angular-ui-router state


【解决方案1】:

(i) 您正在使用 $routerProvider,这不是必需的

(ii) 另外,当我检查您的代码时,您正在加载第二个控制器后加载第一个模块。将您的脚本像这样放在 index.html 中,

<script src="searchRadius.js"></script> 
<script src="getLoc.js"></script>

【讨论】:

  • 我进行了更改,但页面仍然空白,并且我还出现“应用程序不可用...确保您有依赖项...”错误
  • @Thomas 你能给团队查看器吗?
  • 什么是团队查看器?你的意思是像jsfiddle还是什么?如果它在这里工作是页面的网址:alainwebdesign.ca/pl2#/getLocation
  • 我检查了你的代码,你还没有删除第二个控制器上的模块,你也使用 $routerProvider 这不一定是必需的
  • 在 index.html 中将 放在 之前并告诉我跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 2012-11-03
  • 2020-06-25
相关资源
最近更新 更多