【问题标题】:Angular factory function not reached.未达到角工厂功能。
【发布时间】:2015-01-27 10:01:43
【问题描述】:

我正在通过本教程 https://thinkster.io/angulartutorial/angular-rails/ 学习如何将 Angularjs 与 ROR 一起使用

当我为一个服务添加一个新功能时,我已经到了这样的地步,该服务应该获取我们在数据库中的所有帖子。但是,当我运行代码时,页面不再加载。或者至少没有呈现 html。在元素检查中没有任何错误迹象。但是我不确定它是否与将函数放置在 js 文件中有关。我对 js 完全陌生,我仍然很难阅读代码中的 sintax 和发现错误。 Rubymine 为 o.getAll 提供了线索和 Unreachable 代码警告。如果有人可以看看它并给我任何提示,那就太好了。

对一个文件中的所有代码表示歉意,包括 html 和 js。我的资产管道存在一些问题,我打算很快解决。

app.js

块引用

angular.module('flapperNews', ['ui.router'])
//Provider
.config([
    '$stateProvider',
    '$urlRouterProvider',
    function($stateProvider, $urlRouterProvider) {

        $stateProvider
            .state('home', {
                url: '/home',
                templateUrl: '/home.html',
                controller: 'MainCtrl',
                resolve: {
                    postPromise: ['posts', function(posts){
                        return posts.getAll();
                    }]
                }
            })
            .state('posts', {
                url: '/posts/{id}',
                templateUrl: '/posts.html',
                controller: 'PostsCtrl'
            })

        $urlRouterProvider.otherwise('home');
    }])
//Posts service
.factory('posts', ['$http', function($http){

    var o = {
        posts: []
    };
    return o;

    o.getAll = function() {
        return $http.get('/posts.json').success(function(data){
            angular.copy(data, o.posts);
        });
    };


}])

//Main Controller
.controller('MainCtrl', [
    '$scope',
    'posts',

    function($scope, posts){
        $scope.posts = posts.posts;
        $scope.addPost = function(){
            if(!$scope.title  || $scope.title == '') { return; }
            $scope.posts.push({
                title: $scope.title,
                link: $scope.link,
                upvotes: 0,
                comments: [
                    {author: 'Joe', body: 'Cool post!', upvotes: 0},
                    {author: 'Bob', body: 'Great idea but everything is wrong!', upvotes: 0}
                ]
            });
            $scope.title = '';
            $scope.link = '';
        };
        $scope.incrementUpvotes = function(post) {
            post.upvotes += 1;
        };

    }])
//Posts Controller
.controller('PostsCtrl', [
    '$scope',
    '$stateParams',
    'posts',
    function($scope, $stateParams, posts){
        $scope.post = posts.posts[$stateParams.id];

        $scope.addComment = function(){
            if($scope.body === '') { return; }
            $scope.post.comments.push({
                body: $scope.body,
                author: 'user',
                upvotes: 0
            });
            $scope.body = '';
        };}]);

> 并遵循 application.html.erb

> Blockquote

<html>
<head>


  <title>FlapperNews</title>

  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js"></script>
  <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
  <script src="javascripts/app.js"></script>
  <script src="javascripts/application.js"></script>

  <%= csrf_meta_tags %>
</head>
<body ng-app="flapperNews">
<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <ui-view></ui-view>
  </div>
</div>

<script type="text/ng-template" id="/home.html">
  <div class="page-header">
    <h1>Flapper News</h1>
  </div>

  <div ng-repeat="post in posts | orderBy:'-upvotes'">
      <span class="glyphicon glyphicon-thumbs-up"
            ng-click="incrementUpvotes(post)"></span>
    {{post.upvotes}}
      <span style="font-size:20px; margin-left:10px;">
        <a ng-show="post.link" href="{{post.link}}">
          {{post.title}}
        </a>
        <span ng-hide="post.link">
          {{post.title}}
        </span>
      </span>
      <span>
        <a href="#/posts/{{$index}}">Comments</a>
      </span>
  </div>

  <form ng-submit="addPost()"
        style="margin-top:30px;">
    <h3>Add a new post</h3>

    <div class="form-group">
      <input type="text"
             class="form-control"
             placeholder="Title"
             ng-model="title">
    </div>
    <div class="form-group">
      <input type="text"
             class="form-control"
             placeholder="Link"
             ng-model="link">
    </div>
    <button type="submit" class="btn btn-primary">Post</button>
  </form>
</script>

<script type="text/ng-template" id="/posts.html">
  <div class="page-header">
    <h3>
      <a ng-show="post.link" href="{{post.link}}">
        {{post.title}}
      </a>
        <span ng-hide="post.link">
          {{post.title}}
        </span>
    </h3>
  </div>

  <div ng-repeat="comment in post.comments | orderBy:'-upvotes'">
      <span class="glyphicon glyphicon-thumbs-up"
            ng-click="incrementUpvotes(comment)"></span>
    {{comment.upvotes}} - by {{comment.author}}
      <span style="font-size:20px; margin-left:10px;">
        {{comment.body}}
      </span>
  </div>
  <form ng-submit="addComment()"
        style="margin-top:30px;">
    <h3>Add a new comment</h3>

    <div class="form-group">
      <input type="text"
             class="form-control"
             placeholder="Comment"
             ng-model="body">
    </div>
    <button type="submit" class="btn btn-primary">Post</button>
  </form>

</script>

</body>
</html>

【问题讨论】:

    标签: html ruby-on-rails angularjs


    【解决方案1】:

    factory,您在getAll 函数之前返回o,因此无法访问

     .factory('posts', ['$http', function($http){
    
       var o = {
         posts: []
       };
    
       //removed return 
    
       o.getAll = function() {
          return $http.get('/posts.json').success(function(data){
             angular.copy(data, o.posts);
          });
       };
    
      return o; //at the added return 
     }])
    

    【讨论】:

    • @vinibol12 :不客气。如果答案有帮助,您可以投票并接受作为答案。
    • 肯定会这样做。只是你在我发帖后不到 10 分钟就回复了,所以我似乎有最短时间才能接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 2015-11-24
    相关资源
    最近更新 更多