新しいリソース オブジェクトを初期化するのに苦労しています。
REST API からのデータの取得は問題なく機能します。
エラーは、次のコード行でスローされます。
var newRoom = new this.lobbyStorage.LobbyRoom();
次のメッセージが表示されます。
「this.$ngResource は関数ではありません」
私はかなりの数のことを試してきましたが、良い結果につながるものは何もありませんでした.
解決
構文 new Function(...) または単に Function(...) で作成された関数の name プロパティは、空の文字列に設定されます。次の例では、無名関数が作成されるため、name は空の文字列を返します。
関数の名前は変更できません。このプロパティは読み取り専用です。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name
ロビーストレージ.ts
module lobby.services {
export class LobbyStorage {
private baseUrl : string = 'http://localhost:2999'; /*this.appConstant.baseUrl + '/lobby';*/
public static $inject = [
'$http',
'$resource'
];
constructor(private $http: ng.IHttpService, private $ngResource : ng.resource.IResourceService /*,private appConstant*/) {
}
public LobbyRoom() : ng.resource.IResourceClass<ng.resource.IResource<any>> {
return this.$ngResource(this.baseUrl + '/lobby/:id', { id: '@id' });
}
}
}
ロビーモジュール.ts
///<reference path='../../typings/tsd.d.ts' />
module lobby {
'use strict';
/* @ngdoc object
* @name lobby
* @description
*
*/
angular
.module('lobby', [
'ngRoute',
'ngResource'
])
.service('lobbyStorage', lobby.services.LobbyStorage)
/* .constant('appConstant', lobby.constants.Constants.Default);*/
}
ロビーコントローラー.ts
/// <reference path='../_lobby.ts' />
module lobby.controllers {
'use strict';
class LobbyCtrl {
public lobbyData : Array<string>;
public gameCreation : boolean = true;
public currentItem : any = {};
// $inject annotation.
// It provides $injector with information about dependencies to be injected into constructor
// it is better to have it close to the constructor, because the parameters must match in count and type.
// See http://docs.angularjs.org/guide/di
public static $inject = [
'$scope',
'$log',
'lobbyStorage'
];
// dependencies are injected via AngularJS $injector
constructor(private $scope, private $log : ng.ILogService, private lobbyStorage) {
this.init();
}
// Initializer function
private init(){
this.initializeLobbyData();
}
public createRoom() : void{
var newRoom = new this.lobbyStorage.LobbyRoom();
newRoom.name = this.currentItem.name;
newRoom.$save();
}
public initializeLobbyData(){
var res = this.lobbyStorage.LobbyRoom().query(
() => this.lobbyData = res,
() => this.lobbyData[0] = "Error"
);
}
}
/**
* @ngdoc object
* @name lobby.controller:LobbyCtrl
*
* @description
*
*/
angular
.module('lobby')
.controller('LobbyCtrl', LobbyCtrl);
}