在这个标题中:http://plnkr.co/edit/OZa7rvWLQuRHJGiJ8dBL?p=preview
我很难将$http.get
添加到控制器,以便页面运行。
在app.js中 -
我有这段代码:
angular.module('example-app', ['hc.marked', 'hljs'])
.config(['markedProvider', 'hljsServiceProvider', function(markedProvider, hljsServiceProvider) {
// marked config
markedProvider.setOptions({
gfm: true,
tables: true,
sanitize: true,
highlight: function (code, lang) {
if (lang) {
return hljs.highlight(lang, code, true).value;
} else {
return hljs.highlightAuto(code).value;
}
}
});
}])
.controller("MainController", ["$rootScope", "$scope", "marked", function MarkdownController($rootScope, $scope, marked, $http) {
}]);
如何添加此$http.get
以使plunker继续正常工作?
$http.get('http://beta.json-generator.com/api/json/get/VyD_JWT1Q')
.then(function (res) {
$scope.todos = res.data.todo;
$scope.events = res.data.event;
$scope.aboutlongs = res.data.aboutlong;
$scope.mainpoints = res.data.mainpoint;
$scope.tags = res.data.tag;
$scope.galleries = res.data.gallery;
$scope.menus = res.data;
$scope.socials = res.data.social;
});
我试过这个,但它破了:
angular.module('example-app', ['hc.marked', 'hljs'])
.config(['markedProvider', 'hljsServiceProvider', function(markedProvider, hljsServiceProvider) {
// marked config
markedProvider.setOptions({
gfm: true,
tables: true,
sanitize: true,
highlight: function (code, lang) {
if (lang) {
return hljs.highlight(lang, code, true).value;
} else {
return hljs.highlightAuto(code).value;
}
}
});
}])
.controller("MainController", ["$rootScope", "$scope", "marked", function MarkdownController($rootScope, $scope, marked, $http) {
$http.get('http://beta.json-generator.com/api/json/get/VyD_JWT1Q')
.then(function (res) {
$scope.todos = res.data.todo;
$scope.events = res.data.event;
$scope.aboutlongs = res.data.aboutlong;
$scope.mainpoints = res.data.mainpoint;
$scope.tags = res.data.tag;
$scope.galleries = res.data.gallery;
$scope.menus = res.data;
$scope.socials = res.data.social;
});
}]);
提前致谢。
答案 0 :(得分:1)
请在控制器中注入$ http作为依赖项。
.controller("MainController", ["$rootScope", "$scope", "$http", "marked", ....
答案 1 :(得分:1)
更正控制器中依赖注入的顺序(Marked and $http
之间)。应该是这样的:
.controller("MainController", ["$rootScope", "$scope", "$http", "marked", function MarkdownController($rootScope, $scope,$http, marked ) {