动态ui-router> uiSref指令不会生成href

时间:2015-11-06 21:50:26

标签: javascript angularjs angular-ui-router

我花了很多时间,但找不到动态ui-router的解决方案,正好根据“ui-sref”生成“href” 此解决方案存在同样的问题,您可以在此处查看plunker preview

我试着写下这个:

require(['config'], function() {
  require([
    'angular',
    './app',
    './controllers/ListCtrl',
    './controllers/TwoViewsCtrl'
  ], function (angular, app) {
    app.config(['$stateProvider', '$urlRouterProvider',
      function ($stateProvider, $urlRouterProvider) {

      $stateProviderRef = $stateProvider;
      $urlRouterProviderRef = $urlRouterProvider;
    }]);

    app.run(['$http', '$urlRouter', '$state',
      function ($http, $urlRouter, $state) {

        $http.get('/json/routes.json').success(function(data) {
          for (var i = 0; i < data.length; i++) {
            var obj = data[i];

            var getExistingState = $state.get(obj.state);
            if(getExistingState !== null)
              return;

            $stateProviderRef.state(obj.state, obj);
          };

            $urlRouter.sync();
            $urlRouter.listen();
        });

    }]);

    angular.element(document).ready(function() {
      angular.bootstrap(document, ['uiRouts']);
    });

  });
});

或第二种解决方案:

require(['config'], function() {
  require([
    'angular',
    './app',
    './controllers/ListCtrl',
    './controllers/TwoViewsCtrl'
  ], function (angular, app) {
    app.config(['$stateProvider', '$urlRouterProvider',
      function ($stateProvider, $urlRouterProvider) {

      var initInjector = angular.injector(['ng']);
      var $http = initInjector.get('$http');

        $http.get('/json/routes.json').success(function(data) {
          for (var i = 0; i < data.length; i++) {
            var obj = data[i];
                $stateProvider.state(obj.state, obj);
          };
        });

    }]);

    angular.element(document).ready(function() {
      angular.bootstrap(document, ['uiRouts']);
    });

  });
});

但如果我在成功回调函数中使用此解决方案,只需将.JSON放在app.config中,一切正常。

0 个答案:

没有答案