Angular JS /文章没有定义(Angular JS / articles is not defined)

编程入门 行业动态 更新时间:2024-10-28 04:20:30
Angular JS /文章没有定义(Angular JS / articles is not defined)

我在Angular App中的services.js中有这个代码:

.factory('Articles', function($http) { $http.get('https://api.myjson.com/bins/4ici6').then( function(response) { var articles = response.data.articles; }); return { all: function() { return articles; }, get: function(articleId) { for (var i = 0; i < articles.length; i++) { if (articles[i].id === parseInt(articleId)) { return articles[i]; } } return null; } }; })

它不起作用,因为我在控制台中收到此错误:

ReferenceError: articles is not defined at Object.all (http://localhost:8100/js/services.js:31:14) at new <anonymous> (http://localhost:8100/js/controllers.js:4:30) at Object.instantiate (http://localhost:8100/lib/ionic/js/ionic.bundle.js:18015:14)

另外这里是引用articles的controller.js代码:

.controller('NewsCtrl', function($scope, Articles) { $scope.articles = Articles.all(); }) .controller('NewsDetailCtrl', function($scope, $stateParams, Articles) { $scope.article = Articles.get($stateParams.articleId); $scope.posttofacebook = function (){ window.plugins.socialsharing.shareViaFacebook(null, null, $scope.article.url); } $scope.posttotwitter = function (){ window.plugins.socialsharing.shareViaTwitter(null, null, $scope.article.url); } })

我在这做错了什么?

I have this code in services.js in in my Angular App:

.factory('Articles', function($http) { $http.get('https://api.myjson.com/bins/4ici6').then( function(response) { var articles = response.data.articles; }); return { all: function() { return articles; }, get: function(articleId) { for (var i = 0; i < articles.length; i++) { if (articles[i].id === parseInt(articleId)) { return articles[i]; } } return null; } }; })

It doesn't work as I get this error in the console:

ReferenceError: articles is not defined at Object.all (http://localhost:8100/js/services.js:31:14) at new <anonymous> (http://localhost:8100/js/controllers.js:4:30) at Object.instantiate (http://localhost:8100/lib/ionic/js/ionic.bundle.js:18015:14)

Also here is the controller.js code that refers to articles:

.controller('NewsCtrl', function($scope, Articles) { $scope.articles = Articles.all(); }) .controller('NewsDetailCtrl', function($scope, $stateParams, Articles) { $scope.article = Articles.get($stateParams.articleId); $scope.posttofacebook = function (){ window.plugins.socialsharing.shareViaFacebook(null, null, $scope.article.url); } $scope.posttotwitter = function (){ window.plugins.socialsharing.shareViaTwitter(null, null, $scope.article.url); } })

What am I doing wrong here?

最满意答案

因为$http.get是异步调用,所以你必须处理它。

只是将它置于工厂之上将无法始终如一地工作。

试试这个:

.factory('Articles', function($http) { return { all: function() { return $http.get('https://api.myjson.com/bins/4ici6').then(function(response) { return response.data.articles; }); }, get: function(articleId) { //Probably best here to call an API endpoint that will return a single article with the parameter's articleId //Something along the lines of //$http.get('https://api.myjson.com/bins/4ic16/' + articleId).then(function(response) { //handle response}); } }; })

您的控制器也应该更改为处理异步函数调用:

.controller('NewsCtrl', function($scope, Articles) { Articles.all().then(function(articles) { $scope.articles = articles }); })

Because $http.get is an asynchronous call you'll have to deal with that.

Just putting it on top of your factory won't consistently work.

Try this instead:

.factory('Articles', function($http) { return { all: function() { return $http.get('https://api.myjson.com/bins/4ici6').then(function(response) { return response.data.articles; }); }, get: function(articleId) { //Probably best here to call an API endpoint that will return a single article with the parameter's articleId //Something along the lines of //$http.get('https://api.myjson.com/bins/4ic16/' + articleId).then(function(response) { //handle response}); } }; })

Your controller should also be changed to handle the async function call:

.controller('NewsCtrl', function($scope, Articles) { Articles.all().then(function(articles) { $scope.articles = articles }); })

更多推荐

本文发布于:2023-07-31 21:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1348623.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定义   文章   JS   Angular   articles

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!