小编and*_*rew的帖子

AngularJS - Angular UI-Router仅重新加载嵌套视图,而不是父视图

我知道这很长,但我需要在主要问题之前提供一些背景知识.我正在创建一个将分为两列的页面.这是ui-router代码:

  $urlRouterProvider.otherwise("/projects/edit")

  $stateProvider
    .state('projects', {
      url: "/projects",
      template: "<div ui-view></div>"
    })

    .state('projects.edit', {
      resolve: {
        test: function(){
          console.log('projects.edit resolve called')
        }
      },
      url: "/edit",
      templateUrl: "projects.html",
      controller: function($scope, $state){
        $state.go('projects.edit.surveys')

        $scope.transitionToOtherRoute = function () {
            $state.transitionTo('otherRoute')
        }
      }
    })
    .state('projects.edit.surveys', {
      resolve: {
        items: function(){
          var randomNumberArray = [1,2,3,4,5,6]
          //this will just shuffle the array
          for(var j, x, i = randomNumberArray.length; i; j = Math.floor(Math.random() * i), x = randomNumberArray[--i], randomNumberArray[i] = randomNumberArray[j], randomNumberArray[j] = x);
          return randomNumberArray
        }
      }, …
Run Code Online (Sandbox Code Playgroud)

angularjs angular-ui-router

17
推荐指数
1
解决办法
1万
查看次数

AngularJS Karma测试 - 测试时未定义传递给控制器​​的Resolve对象

我们正在使用ui-router 0.2.10.

我将一个resolve对象作为参数注入我的控制器,然后在控制器中设置一个范围变量.它在应用程序上完美运行如下:

国家提供者

$stateProvider.state('myState', {
      resolve:{
         foo:  function(){
            return 'bar';
         },
      url: '/',
      templateUrl: 'index.html',
      controller: 'FooCtrl'
   })
Run Code Online (Sandbox Code Playgroud)

调节器

app.Controllers.controller('FooCtrl', ['$scope', '$state', 'foo',
    function ($scope, $state, $log, Zone, foo) {
        $scope.testVar = foo
        console.log($scope.testVar);
    }])
Run Code Online (Sandbox Code Playgroud)

然后,按照预期在Chrome中将"条形图"记录到控制台.

但是当使用Karma运行测试时,解析对象现在是未定义的,这使测试失败.这是测试代码:

describe('controllers', function(){

  var $rootScope, 
      $scope,
      $state

  beforeEach(module('app'))

  beforeEach(inject(function($injector) {
    $state = $injector.get('$state')
    $rootScope = $injector.get('$rootScope')
    $scope = $rootScope.$new()
    $controller = $injector.get('$controller')
  }))

  it('FooCtrl should exist', inject( function() {
    $state.go('myState')
    $rootScope.$apply()

    $controller = $controller('FooCtrl', {
      '$scope': $scope
    })
    $rootScope.$apply()

    assert.equal($scope.testVar, "bar", "these strings …
Run Code Online (Sandbox Code Playgroud)

javascript angularjs angular-ui-router

6
推荐指数
1
解决办法
2718
查看次数

标签 统计

angular-ui-router ×2

angularjs ×2

javascript ×1