gpt4 book ai didi

javascript - $在 AngularJs 中注入(inject)

转载 作者:行者123 更新时间:2023-12-03 03:38:59 25 4
gpt4 key购买 nike

我在向 Controller 注入(inject)服务时遇到问题。下面是代码和此操作的结果。

user.service.js:

class UsersService{
constructor(){}
usersServiceFn () {
return {
name: 'TEST'
};
};
}
export default UsersService;

users.jsp:

import UsersService from './users.service';

export default app => {
app.factory('users', UsersService);

if (ENVIRONMENT === 'test') {
require('./users.test.js');
}
}

service.js:

import usersService from './users/users';

export default app => {
INCLUDE_ALL_MODULES([usersService], app);
}

index.js:

import angular from 'angular';
import angularUIRouter from 'angular-ui-router';
import appComponents from './components/components.js';
import commonComponents from './common/components.js';
import appServices from './services/services.js';
import appConfiguration from './app.config';

// Single Style Entry Point
import './index.scss';

if (ENVIRONMENT === 'test') {
console.log('ENV:', ENVIRONMENT);
require('angular-mocks/angular-mocks');
}

const app = angular.module('app', ['ui.router']);
appComponents(app);
commonComponents(app);
appServices(app);

home.controller.js:

 class HomeController {

constructor($scope, $http, usersService) {
this.$scope = $scope;
this.$http = $http;
this.usersService = usersService;
this.$scope.persons = null;
this.name = 'home';
this.$scope.surname ='ZZZZZZ';
this.$scope.randomNumb = Math.random();
this.initRecords();
this.range();
this.watchText();
}

/..../


}
HomeController.$inject = ['$scope', '$http', 'usersService'];
export default HomeController;

结果:

enter image description here

我正在尝试,但我不知道问题出在哪里。

**结构:** enter image description here

最佳答案

改变这个

app.factory('users', UsersService);

app.factory('usersService', UsersService);

关于javascript - $在 AngularJs 中注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45722004/

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