我有三個文件 - app.js,controllers.js,services.js。如果我有辦法工作,還有其他一些我想添加的。我看到代碼進入mainApp的angular.module部分,然後是服務和控制器。然而,沒有錯誤發生,沒有數據回來,並且控制器方法根本不被調用(只有控制器從它的外觀初始化)。我嘗試使用init()強制進行一個調用,但沒有運氣。我究竟做錯了什麼?控制器沒有加載JSON文件Angular
這裏是app.js
var mainApp = angular.module('MainApp', ['test.services', 'test.controllers']);
Controllers.js
var app = angular.module('test.controllers', []);
app.controller('TestCtrl', [$scope, 'TestService', function($scope, TestService)
var promise = TestService.makeHttpRequest(../../src/testFile.json);
promise.then(function(data){
$scope.results = data;
console.log($scope.results);
});
}]);
不工作。下面的方法也沒有得到任何數據。
var app = angular.module('test.controllers', []);
app.controller('TestCtrl', [$scope, $http, function($scope, $http){
$http.get('../../src/testFile.json').then(function(data) {
$scope.results = data.data;
console.log(data);
});
}]);
services.js
var appServices = angular.module('test.services', []);
//Does the same thing, but get call moved to services
的Index.html
<body class="bg-blue mainContainer" ng-app="MainApp">
<div id="content-container" ng-contoller="TestCtrl" class="container main-container">{{results}}</div>
<div id="content-container" ng-contoller="MainController" class="container main-container" ng-init="loadResults()">
</body>
刪除依賴項test.services和test.controllers,這是不需要的,使那[] –