小编leo*_*eog的帖子

测试AngularJS范围变量

我正在尝试实现依赖于范围变量的测试.我想启用ng-switch-when来解析表达式.这就是我要做的事情(使用$ rootScope 更新):

it('should switch on array changes', inject(function($rootScope, $compile) {
  element = $compile(
    '<div ng-switch="select">' +
      '<div ng-switch-when="test[0]">test[0]:{{test[0]}}</div>' +
    '</div>')($rootScope);
  expect(element.html()).toEqual('<!-- ngSwitchWhen: test[0] -->');
  $rootScope.test = ["leog"];
  $rootScope.select = "leog";
  $rootScope.$apply();
  expect(element.text()).toEqual('test[0]:leog');
}));
Run Code Online (Sandbox Code Playgroud)

我的问题是,我为此工作的实现没有得到范围变量"test"来评估和工作,因为我期望.这是实施:

var ngSwitchWhenDirective = ngDirective({
  transclude: 'element',
  priority: 800,
  require: '^ngSwitch',
  compile: function(element, attrs) {
    return function(scope, element, attr, ctrl, $transclude) {
      var expr = scope.$eval(attrs.ngSwitchWhen),
          ngSwitchWhen = expr !== undefined ? expr : attrs.ngSwitchWhen;
      ctrl.cases['!' + ngSwitchWhen] = (ctrl.cases['!' + …
Run Code Online (Sandbox Code Playgroud)

unit-testing angularjs ng-switch angularjs-directive angularjs-scope

5
推荐指数
1
解决办法
1811
查看次数

具有多个ui视图的多模块嵌套ui-router

我正在研究基于Angular和Angular UI路由器的复杂UI架构.我正在尝试在多个模块上定义具有嵌套ui-views的路由.

以下是我要做的事情:http://plnkr.co/edit/sxJOPQAgyEZfcYfUReGR?p = preview

这里的文件:

的index.html

<body ng-app="app">
    <h1 ui-view="title"></h1>
    <div ui-view="sidebar1"></div>
    <ul ui-view="sidebar2"></ul>
    <div ui-view="content"></div>
</body>
Run Code Online (Sandbox Code Playgroud)

app.js

angular
.module('app', ['ui.router', 'page1'])
.config(['$locationProvider','$stateProvider', '$urlRouterProvider',
  function ($locationProvider, $stateProvider, $urlRouterProvider) {
    $locationProvider.html5Mode(true);
    $stateProvider.state({
      name: "page1",
      url: "/page1",
      abstract: true
    });
    $urlRouterProvider.otherwise('/page1');
}]);
Run Code Online (Sandbox Code Playgroud)

page1.js

angular
.module('page1', ['ui.router'])
.config(function ($stateProvider) {
    $stateProvider.state({
        name: 'page1.main',
        views: {
            title: {
                template: "My Title"
            },
            sidebar1: {
                template: "<ul><li ng-repeat='item on items'>{{item}}</li></ul>",
                controller: function($scope) {
                    $scope.items = ['foo1', 'bar1'];
                }
            },
            sidebar2: { …
Run Code Online (Sandbox Code Playgroud)

module uiview angularjs angular-ui-router

3
推荐指数
1
解决办法
8796
查看次数