I have a main page that consists of only two parts: a navigation headline, and the dynamic content.
index.html:
<div ng-controller='MainCtrl'>
<li ng-repeat="nav in navs">
<a href="#{{nav.url}}">{{nav.name}}</a>
</li>
</div>
<div ng-view></div> <!-- replaced by ngRoute -->
The navigation is achieved as follows:
app.config(function($routeProvider, $locationProvider) {
$routeProvider.when("/test", {
templateUrl : "templates/test.html",
controller : "testController"
});
});
The headline links should be provided by the backend on each webservice request.
Question: how can I trigger the following init
function from within another controller (the one receiving the get response)? I lateron want to trigger this method from within different controllers.
app.controller('MainCtrl', ['$scope', function($scope) {
this.init = function(data) {
$scope.navs = data;
}
}]);
I tried the following, which did not work:
test.html:
<div>
<h1>some stuff provided by testController.js</h1>
</div>
testController.js:
angular.module('test').controller('testController', ['$scope', '$http', '$controller', function($scope, $http, $controller) {
$http.get(url)
.success(function(data) {
$controller('MainCtrl').init(data.mynavigation); //assume navigations exists
//process content in data
});
}]);
Result:
Error: [$injector:unpr] http://errors.angularjs.org/1.4.3/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20MainCtrl