2017-04-18 35 views
0

我正在寫這個來獲得有關AngularJS的問題的幫助。這是我編程教育中的一個重複問題,因爲無論如何,我在編碼方面最大的問題是如何將文件鏈接在一起。在AngularJS中,我一直對我感到不安,因爲我一直試圖讓我的代碼工作幾個月,甚至無法將它連接到基本級別,所以編寫更多代碼是沒有用的。我無法獲取AngularJS來加載我的服務

我會發布我的HTML和我所有的AngularJS文件供您查看,並請告訴我我做錯了什麼,以及如何防止這種情況發生。我在做一個codecademy fullstack課程,我也需要添加ReactJS和Jquery。我去Jquery過渡到我的網站的所有其他部分,但我擔心這將是一個問題,一旦我嘗試把它放在這裏。

P.s.我不知道如何在JSfiddle中創建多個Javascript文件,但我希望它能做到。

代碼:

var app = angular.module("SugesstionBox", []); 

// my HomeController.js file 

app.controller("HomeController", ['$scope', 'suggestions' function($scope, suggestions){ 
    $scope.posts = suggestions.posts; 
    $scope.message = "AngularJS Tutorial"; 
}]); 

// My suggestions.js service file 

app.factory('suggestions', [function(){ 
    var demosuggestions = { 
     posts: [ 
      { 
      title: 'free pizza is amazing!', 
      upvotes: 1, 
      comments: [], 
      }, 
      { 

      title: 'free pizza is still amazing!', 
      upvotes: 5, 
      comments: [], 
      } 

     ] 

    }; 
    return demosuggestions; 
}]); 

Complete example on JSFiddle.

+1

你已經錯過了在控制器定義一個逗號。這裏是你的工作小提琴:https://jsfiddle.net/9znr99ku/1/ – tanmay

+0

哦,謝謝你,謝謝你!我知道它必須是小事,但我沒有看到它。它現在有效。我希望能夠最終繼續前進。由於這種愚蠢的錯誤,我浪費了很多時間。我現在可以被僱用了! –

+0

很高興幫助:) – tanmay

回答

0

AS tanmay說你錯過了正確的聲明,並在你的撥弄你的腳本,這是不可用的小提琴..... 更重要的是,我建議你使用ng -if的評論顯示更好的觀點像下面和它是post.upvotespost.upvote

var app = angular.module('plunker', []); 
 

 
app.controller("HomeController", ['$scope', 'suggestions', function($scope, suggestions){ 
 
    $scope.posts = suggestions.posts; 
 
    $scope.message = "AngularJS Tutorial"; 
 
}]); 
 
app.factory('suggestions', [function(){ 
 
    var demosuggestions = { 
 
     posts: [ 
 
      { 
 
      title: 'free pizza is amazing!', 
 
      upvotes: 1, 
 
      comments: [], 
 
      }, 
 
      { 
 
       
 
      title: 'free pizza is still amazing!', 
 
      upvotes: 5, 
 
      comments: [], 
 
      } 
 
      
 
     ] 
 
     
 
    }; 
 
    return demosuggestions; 
 
}]);
<!DOCTYPE html> 
 
<html ng-app="plunker"> 
 

 
    <head> 
 
    <meta charset="utf-8" /> 
 
    <title>AngularJS Plunker</title> 
 
    <script>document.write('<base href="' + document.location + '" />');</script> 
 
    <link rel="stylesheet" href="style.css" /> 
 
    <script data-require="[email protected]" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script> 
 
    <script src="app.js"></script> 
 
    </head> 
 

 
    <body ng-controller="HomeController"> 
 
    <div> 
 
      {{ message }} 
 
     </div> 
 
     <div ng-repeat="post in posts"> 
 
     <div> 
 
     {{post.title}}</div> 
 
     <div> 
 
     {{post.upvotes}}</div> 
 
     <div ng-if="post.comments.length>0"> 
 
     {{post.comments}}</div> 
 
     </div> 
 
    </body> 
 

 
</html>

相關問題