单击链接两次以使用ui路由器激活状态

时间:2015-11-29 21:57:23

标签: javascript angularjs angular-ui-router

最初 schoolyears 状态处于活动状态。

当我点击按钮激活 schoolyears.create 状态时,我必须执行此操作TWICE。只有这样才会呈现创建schoolyear视图。

我错了什么?

INDEX.HTML

// navbar removed for brevity

<div ui-view="content" class="container body-content">
        CONTENT
</div>

app.js

'use strict';
angular
  .module('TGB', ['ui.router', 'ui.bootstrap'])

  .run(['$rootScope', '$state', '$stateParams',
    function ($rootScope, $state, $stateParams) {

        $rootScope.$state = $state;
        $rootScope.$stateParams = $stateParams;

    }
  ])
  .config(function ($stateProvider, $urlRouterProvider) {

      //localStorageServiceProvider.setPrefix('TGB');
      $urlRouterProvider.otherwise("schoolyears");
      $stateProvider
        .state('schoolyears', {
            url: '/schoolyears',
            views: {
                'content@': {
                    templateUrl: "/js/schoolyears/schoolyears.html",
                    controller: 'SchoolyearsController'
                }
            }
        })
        .state('schoolyears.selected', {
            url: '/:id'
        }) 
        .state('schoolyears.create', {
            url: '/create',
            views: {
                'content@': {
                    templateUrl: '/js/schoolyears/createSchoolyear.html',
                    controller: 'CreateSchoolyearController'
                }
            }
        })
  });

SchoolyearsController.js

(function () {
    'use strict';

    angular
      .module('TGB')
      .controller('SchoolyearsController', SchoolyearsController);

    function SchoolyearsController($scope, $state) {

        $scope.schoolyears = []; 
    }
})();

CreateSchoolyearController.js

(function () {
    'use strict';

    angular
      .module('TGB')
      .controller('CreateSchoolyearController', CreateSchoolyearController);

     function CreateSchoolyearController($scope, $state) {



    }
})();

1 个答案:

答案 0 :(得分:0)

我想说,只需设置如下状态

    .state('schoolyears.create', {
        url: '/create',
        views: {
            'content@schoolyears': {
                templateUrl: '/js/schoolyears/createSchoolyear.html',
                controller: 'CreateSchoolyearController'
            }
        }
    })