gpt4 book ai didi

javascript - 在 AngularJS 测试服务中使用下划线

转载 作者:行者123 更新时间:2023-11-29 10:44:38 25 4
gpt4 key购买 nike

<分区>

我正在学习 AngularJS 教程的第 5 步,并在测试部分遇到了这个片段:

describe('PhoneCat controllers', function() {

describe('PhoneListCtrl', function(){
var scope, ctrl, $httpBackend;

// Load our app module definition before each test.
beforeEach(module('phonecatApp'));

// The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
// This allows us to inject a service but then attach it to a variable
// with the same name as the service.
beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
$httpBackend = _$httpBackend_;
$httpBackend.expectGET('phones/phones.json').
respond([{name: 'Nexus S'}, {name: 'Motorola DROID'}]);

scope = $rootScope.$new();
ctrl = $controller('PhoneListCtrl', {$scope: scope});
}));

在注入(inject) $httpBackend 时,我不完全理解下划线的用途。我看到评论并了解代码在做什么。我只是不明白为什么我们只用 $httpBackend 来做这件事。

我们正在注入(inject)的另外两个服务不需要以这种方式注入(inject)。我们如何通过以迂回方式注入(inject) $httpBackend 然后立即将其分配给同名变量来帮助自己,我们不能直接注入(inject)它吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com