小编cam*_*kid的帖子

为什么不能在具有隔离范围的指令的模板中访问$ rootScope?

对于隔离范围,指令的模板似乎无法访问控制器('Ctrl')$ rootScope变量,但该变量确实出现在指令的控制器中.我理解为什么控制器('Ctrl')$ scope变量在隔离范围内不可见.

HTML:

<div ng-app="app">
    <div ng-controller="Ctrl">
        <my-template></my-template>
    </div>

    <script type="text/ng-template" id="my-template.html">
        <label ng-click="test(blah)">Click</label>
    </script>
</div>
Run Code Online (Sandbox Code Playgroud)

JavaScript的:

angular.module('app', [])
    .controller('Ctrl', function Ctrl1($scope,  $rootScope) {
        $rootScope.blah = 'Hello';
        $scope.yah = 'World'
    })
    .directive('myTemplate', function() {
        return {
            restrict: 'E',
            templateUrl: 'my-template.html',
            scope: {},
            controller: ["$scope", "$rootScope", function($scope, $rootScope) {
                console.log($rootScope.blah);
                console.log($scope.yah);,

                $scope.test = function(arg) {
                    console.log(arg);
                }
            }]
        };
    });
Run Code Online (Sandbox Code Playgroud)

的jsfiddle

访问变量时没有隔离范围 - 通过注释隔离范围行可以看出:

        // scope: {},
Run Code Online (Sandbox Code Playgroud)

javascript angularjs angularjs-directive

80
推荐指数
3
解决办法
9万
查看次数

node.js - request - 如何"emitter.setMaxListeners()"?

当我使用node.js'request'模块对某个URI进行GET时;

var options = {uri:"aURI", headers:headerData};
request.get(options, function (error, response, body) {
}
Run Code Online (Sandbox Code Playgroud)

错误消息是:

[Error: Exceeded maxRedirects. Probably stuck in a redirect loop.]
Run Code Online (Sandbox Code Playgroud)

并且还有以下消息:

"(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit."
Run Code Online (Sandbox Code Playgroud)

我怎么样setMaxListeners

httprequest node.js

39
推荐指数
4
解决办法
7万
查看次数

将全局变量传递给函数是否有问题?

考虑以下函数声明:

int abmeld(char *strsend)
Run Code Online (Sandbox Code Playgroud)

这就是这样称呼的

abmeld(str);
Run Code Online (Sandbox Code Playgroud)

其中str是在程序文件开头(包含之后)声明和初始化的全局变量,如下所示:

char str[300] = "";
Run Code Online (Sandbox Code Playgroud)

现在我已经知道这是不必要的代码(你可以从任何函数中访问和修改char数组而不用传递它),但这实际上是否有问题呢?

是否存在可能因将已经全局范围的变量传递给函数而导致的后果(如硬错误可能性或未定义的行为)?

c

22
推荐指数
5
解决办法
8663
查看次数

AngularJS 1.3 - Datepicker初始格式不正确

我昨天开始使用AngularJS 1.3,我发现的一个问题是Datepicker中的初始日期不是定义的格式.选择日期,日期的格式是正确的.

AngularJS 1.2.10的初始格式是正确的,可以通过注释掉脚本标记在示例中看到.

如何将初始格式设置为正确?

Plunker的例子

HTML

<!doctype html>
<html ng-app="plunker">
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.0/angular.min.js"></script>
    <!-- <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.js"></script> -->
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.js"></script>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

  <br>
  <br>
  <div ng-controller="DatepickerDemoCtrl">
    <div class="row">
        <div class="col-md-6">
            <p class="input-group">
              <input type="text" class="form-control" datepicker-popup="{{format}}" ng-model="dt" is-open="opened" min-date="minDate" max-date="'2015-06-22'" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close" />
              <span class="input-group-btn">
                <button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
              </span>
            </p>
        </div>
    </div>
  </div>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

JS

var app = angular.module('plunker', ['ui.bootstrap']);
app.controller("DatepickerDemoCtrl", function ($scope) {
  $scope.format …
Run Code Online (Sandbox Code Playgroud)

twitter-bootstrap angularjs

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

在类中存储不同泛型类型的列表

班级结构

我附上了一张我想要做的事情的照片.假设我在课堂上有一个T列表

public class MyClass<T> 
    where T : IMyInterface
{
    public List<T> list = new List<T>;
}
Run Code Online (Sandbox Code Playgroud)

现在,另一个类有一个MyClass列表.

public class AnotherClass
{
    public List<MyClass<IMyInterface>> list = new List<MyClass<IMyInterface>>;
}
Run Code Online (Sandbox Code Playgroud)

我应该为MyClass提供什么?如果我把T,那么它假设该类中的所有类型都相同,但事实并非如此.如果我把IMyInterface,我无法在访问这些类时将IMyInterface转换为T.

new AnotherClass().list.Add(new MyClass<T>()); 
Run Code Online (Sandbox Code Playgroud)

现在这里的类型是什么?AnotherClass泛型类型中的列表是IMyInterface,但我要添加的是T,TI希望它是动态的,但它们仍然在IMyInterface下.

c# generics class list dynamic

19
推荐指数
3
解决办法
5257
查看次数

"*ngFor"背景颜色变化中的Angular 2"ng-style"

我正在尝试使用背景颜色ng-style.列的每一行都是使用生成的ngFor.每个项目都有不同的背景颜色

<ion-item class="category-list" *ngFor="let item of character['items']">
   <ion-avatar item-left>
  <i class="icon" [ngStyle]="{'background-color': item.bgcolor}"><img src="../img/icons/category/{{item.img}}.png"></i>
  </ion-avatar>
    <h2>{{item.category}}</h2>
  </ion-item>
Run Code Online (Sandbox Code Playgroud)

而Typescript.ts:

 var characters = [
  {
    name: 'Gollum',
    quote: 'Sneaky little hobbitses!',
   items: [
      { bgcolor: 'fb9667', img: 'airTicket', category: 'Air tickets' },
      { bgcolor: '000000', img: 'airTicket', category: 'Beauty/Fitness'},
      { bgcolor: '0b9660', img: 'airTicket', category: 'Bike'}
    ]
  },
]
Run Code Online (Sandbox Code Playgroud)

不知道如何应用颜色代码列表.

javascript css typescript ionic2 angular

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

如何对这种效果进行单元测试(使用{dispatch:false})?

ngrx和单元测试初学者在这里.我有以下效果:

@Injectable()
export class NotificationEffects {
  @Effect({dispatch: false})
  notificationShow$ = this.actions$
    .ofType(notificationAction.NOTIFICATION_SHOW)
    .do((action: notificationAction.NotificationShowAction) => {
      this.notificationService.info(action.payload.config);
    });

  constructor(private actions$: Actions, private notificationService: NotificationService) {}
}
Run Code Online (Sandbox Code Playgroud)

具体来说,我想测试一下是否调用了notificationService方法信息.我该怎么办?

我已经按照这些示例但没有找到解决方案:

https://netbasal.com/unit-test-your-ngrx-effects-in-angular-1bf2142dd459 https://medium.com/@adrianfaciu/testing-ngrx-effects-3682cb5d760e https://github.com/ngrx /effects/blob/master/docs/testing.md

unit-testing ngrx-effects angular

10
推荐指数
2
解决办法
2341
查看次数

从Firebase获取随机物品

我搜索了它,但所有答案都很旧,所以也许有更好的方法.我试图从Firebase DB中获取一个随机项目,如下所示:

在此输入图像描述

我想得到一个随机用户,就是这样.

有任何想法吗?

javascript firebase firebase-realtime-database

8
推荐指数
2
解决办法
8073
查看次数

如何从两个接口的联合创建类型?

考虑以下:

interface Man {
  name: string
}

interface Dog {
  breed: string
}

let manDog: Man | Dog;
Run Code Online (Sandbox Code Playgroud)

是否可以创建一种ManDog等效于的类型,Man | Dog以便可以执行此操作:

let manDog: ManDog;
Run Code Online (Sandbox Code Playgroud)

typescript

7
推荐指数
1
解决办法
5020
查看次数

为什么Angular不推荐"导出默认值"?

根据Typescript文档("结构模块指南"部分):

如果您只导出单个类或函数,请使用export default

正如"顶级附近的出口"减少了模块消费者的摩擦,引入默认导出也是如此.如果模块的主要用途是容纳一个特定的导出,那么您应该考虑将其导出为默认导出.这使导入和实际使用导入更容易一些.

例:

export default class SomeType {
  constructor() { ... }
}
Run Code Online (Sandbox Code Playgroud)

组件Angular文档中(例如),可以看到:

export class HeroListComponent implements OnInit {
  heroes: Hero[];
  selectedHero: Hero;

  constructor(private service: HeroService) { }

  ngOnInit() {
    this.heroes = this.service.getHeroes();
  }

  selectHero(hero: Hero) { this.selectedHero = hero; }
}
Run Code Online (Sandbox Code Playgroud)

通常,组件或模块的主要目的是容纳一个特定的导出.那么,Angular有没有使用或推荐使用的原因export default

typescript angular

7
推荐指数
1
解决办法
2841
查看次数