javascript - 使用 $scope - Angular Js

标签 javascript angularjs twitter-bootstrap

我了解了 $scope 的含义,从现在开始我将在我的代码中使用它。但是,当我尝试读取 json 对象时,浏览器中没有显示任何内容。代码位于显示的链接中。

https://jsfiddle.net/jgou7j0p/

<div class="container">
    <div class="row row-content" ng-controller="DishDetailController">
        <div class="col-xs-12">
            <div class="media">
              <div class="media-left media-middle">
                 <img ng-src={{dish.image}} class="media-object img-thumbnail">
              </div>
              <div class="media-body">
                  <div class="media-heading">
                  <h2>{{dish.name}}
                    <span class="label label-danger label-xs"> {{dish.label}}</span> 
                    <span class="badge"> {{dish.price | currency}}</span>
                  </h2>
                  <p>{{dish.description}}</p>
                  </div>    
              </div>    
            </div>
        </div>
        <div class="col-xs-9 col-xs-offset-1">
            <div class = "row">
            <div class = "col-xs-6">
              <h3>Customer Comments</h3>
            </div>
            <div class = "col-xs-6" style="margin-top: 20px;">
              <form class="form-inline" role="form">
                <div class="form-group">
                  <label class="control-label">Sort by:</label>
                  <input type="text" class="form-control" ng-model="sorting">
                </div>
              </form>
            </div>
          </div>
            <div class="row">
                <div class="col-xs-12">
                  <blockquote ng-repeat="comment in dish.comments | orderBy: sorting">
                    <p>{{comment.rating}} Stars</p>
                    <p>{{comment.comment}}</p>  
                    <footer>{{comment.author}}, {{comment.date | date:'MMM. dd, yyyy'}}</footer> 
                  </blockquote>
                </div>
            </div>
        </div>
    </div>

</div>

和js代码。

<script>

    angular.module('confusionApp',[])

           .controller('DishDetailController',['$scope', function($scope) {

            $scope.dish={
                          name:'Uthapizza',
                          image: 'images/uthapizza.png',
                          category: 'mains', 
                          label:'Hot',
                          price:'4.99',
                          description:'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.',
                           comments: [
                               {
                                   rating:5,
                                   comment:"Imagine all the eatables, living in conFusion!",
                                   author:"John Lemon",
                                   date:"2012-10-16T17:57:28.556094Z"
                               },
                               {
                                   rating:4,
                                   comment:"Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
                                   author:"Paul McVites",
                                   date:"2014-09-05T17:57:28.556094Z"
                               },
                               {
                                   rating:3,
                                   comment:"Eat it, just eat it!",
                                   author:"Michael Jaikishan",
                                   date:"2015-02-13T17:57:28.556094Z"
                               },
                               {
                                   rating:4,
                                   comment:"Ultimate, Reaching for the stars!",
                                   author:"Ringo Starry",
                                   date:"2013-12-02T17:57:28.556094Z"
                               },
                               {
                                   rating:2,
                                   comment:"It's your birthday, we're gonna party!",
                                   author:"25 Cent",
                                   date:"2011-12-02T17:57:28.556094Z"
                               }

                           ]
                    };

            $scope.dish = dish;
            $scope.sorting = '';

        }]);

有什么想法可能是错误的吗?

谢谢

西奥。

最佳答案

您将 $scope.dish 设置为 and object,但随后又执行 $scope.dish = duck?那是你的问题。

删除最后的$scope.dish = duck,你应该就可以了。

Here是一个 fiddle 来说明。

正如 @Makoto 在上面的评论中恰本地提到的,如果您正在学习 Angular,您不妨尝试使用 controller as syntax 表示法。看看this very good post了解更多信息。祝你好运!

关于javascript - 使用 $scope - Angular Js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42372272/

相关文章:

html - 列 Bootstrap 内的中心 div

javascript - 多维数组括号表示法的正则表达式

angularjs $timeout 最大值

angularjs - 如何使指令独立于模块

javascript - $emit Controller 中无法访问的数据

ruby-on-rails - Rails 3.2.8 中 Twitter Bootstrap 和 jQuery.ui 冲突

javascript - 如何清理用户输入文本以便它可以在 Javascript/JSON 中使用?

javascript - 相同的 Controller ,不同的模板,一个新变量

javascript - 如何制作多种颜色的指示器

javascript - 下拉菜单不会保持打开状态 bootstrap 4