意见箱应用程序不会显示建议

时间:2018-05-04 13:54:21

标签: javascript html angularjs

我尝试使用AngularJS 1.X在JavaScript中创建一个建议框应用程序,人们可以发布建议并提出建议。我无法获得显示在页面上的建议。救命?这是我的代码:

的index.html:

<!DOCTYPE html>
<html>
<Head>
    <title>Suggestion Box</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
    <script type="text/javascript" src="https://code.angularjs.org/1.2.28/angular-route.min.js"></script>
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
</Head>
<body ng-app="SuggestionBox" ng-controller="HomeController">
    <h1> Suggestion Box </h1>
    <form ng-submit="addSuggestion()" style="margin-top: 50px">
        <h3> Submit Your Suggestion </h3>
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Great ideas here" ng-model="title"></input>
        </div>
        <button type="submit" class="btn btn-primary">Suggest</button>
    </form>
    <div class="main" ng-repeat="post in posts"></div>
    <p>
        <span class="glyphicon glyphicon-plus-sign" ng-click="upVote(post)"></span> Upvotes: <span id="$scope.posts.upvotes"></span>
    </p>

    <!-- Modules -->
    <script type="text/javascript" src="C:\Users\Olivia\Documents\JS\app.js"></script>

    <!-- Controllers -->
    <script type="text/javascript" src="C:\Users\Olivia\Documents\JS\controllers\HomeController.js"></script>

    <!-- Services -->
    <script type="text/javascript" src="C:\Users\Olivia\Documents\JS\Services\Suggestions.js"></script>

</body>
</html>

控制器:

app.controller('HomeController', ['$scope', 'suggestions', function($scope, suggestions) {
    $scope.helloWorld = "Hello, AngularJS!",
    $scope.posts = Suggestions.posts,
    $scope.addSuggestion = function($scope.title) {
        if(!$scope.title || $scope.title === "") {
            return;
        };
        $scope.posts.push({
            title: $scope.title,
            upvotes: 0,
        });
        $scope.title = '';
    }
    $scope.upVote = function(post) {
        $scope.posts[post].upvotes += 1;
    };

}]);

app.js:

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

我还没有添加任何路由,所以路线只是坐在那里

Suggestions.js:

app.factory('Suggestions', [function(){
    var demoSuggestions  = {
        posts: [
            {
                title: 'Insert suggestion here', 
                upvotes: 10,
                comments: [],
            },
            {
                title: 'See above',
                upvotes: 2,
                comments: [],
            },
        ]
    };
    return demoSuggestions;
}]);

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

这是一个有效的版本。看起来你是AngularJS的新手。随意提出任何问题。

&#13;
&#13;
"use strict";
var app = angular.module('SuggestionBox', ['ngRoute']);

app.factory('SuggestionsFactory', [function () {
    var posts = [
        {
            title: 'Insert suggestion here',
            upvotes: 10,
            comments: [],
            id: 1
        },
        {
            title: 'See above',
            upvotes: 2,
            comments: [],
            id: 2
        }
    ];
    function postFactory(title) {
        return {
            title: title,
            upvotes: 0,
            comments: []
        }
    }
    var getPosts = function(){
        return angular.copy(posts);
    }
    var addPost = function (title) {
        posts.push(postFactory(title));
    }
    var removePost = function(id){
        // remove from posts by id
    }
    return {
        getPosts: getPosts,
        removePost: removePost,
        addPost: addPost
    }
}]);

app.controller('HomeController', ['$scope', 'SuggestionsFactory', function ($scope, SuggestionsFactory) {
    var vm = this;
    vm.posts = SuggestionsFactory.getPosts();
    vm.title = '';
    vm.addSuggestion = function () {
        if (vm.title && vm.title !== '') {
            SuggestionsFactory.addPost(vm.title);
            vm.posts = SuggestionsFactory.getPosts();
            vm.title = '';
        }
    }
    vm.upVote = function (post) {
        post.upvotes += 1;
    };
}]);
&#13;
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
    <script type="text/javascript" src="https://code.angularjs.org/1.2.28/angular-route.min.js"></script>
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="style.css">
    <title>Document</title>
</head>

<body ng-app="SuggestionBox" ng-controller="HomeController as vm" class="container">
    <div class="row">
        <div class="col-sm-12">
            <h1> Suggestion Box </h1>
            <ng-form style="margin-top: 50px">
                <h3> Submit Your Suggestion </h3>
                <div class="form-group">
                    <input type="text" class="form-control" placeholder="Great ideas here" ng-model="vm.title" />
                </div>
                <button type="button" class="btn btn-primary" ng-click="vm.addSuggestion(vm.title)">Suggest</button>
            </ng-form>
            <div class="main" ng-repeat="post in vm.posts">
                <p style="font-weight:bold;">{{post.title}}</p>                
                <div>
                    <button ng-click="vm.upVote(post)">upvote</button> Upvotes:
                    <span id="upvotes">{{post.upvotes}}</span>
                </div>
            </div>
        </div>
    </div>
</body>

</html>
&#13;
&#13;
&#13;