简体   繁体   English

使用$ scope-Angular Js

[英]Using $scope - Angular Js

I learned what $scope means,and now from now on I am going to use it in my code. 我了解了$ scope的含义,从现在开始,我将在代码中使用它。 However,nothing is displayed in my browser,when I am trying to read the json objects. 但是,当我尝试读取json对象时,浏览器中什么也没有显示。 The code is in the link shown. 代码在显示的链接中。

https://jsfiddle.net/jgou7j0p/ 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>

and the js code. 和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 = '';

        }]);

Any ideas what could be wrong? 任何想法可能有什么问题吗?

Thanks, 谢谢,

Theo. 西奥

You are setting $scope.dish to and object but then you do $scope.dish = dish ? 您将$scope.dish设置$scope.dish和对象,但随后您执行$scope.dish = dish吗? That is your problem. 那是你的问题。

Remove the final $scope.dish = dish and you should be good. 删除最后的$scope.dish = dish ,您应该会很好。

Here is a fiddle to illustrate. 是一个小提琴来说明。

As aptly mentioned in a comment above by @Makoto, if you are learning Angular you may as well try to use controller as syntax notation. 正如@Makoto在上面的评论中适当提到的那样,如果您正在学习Angular,则最好尝试使用controller as syntax表示法。 Look and this very good post to learn more about it. 外观和这篇很好的帖子,以了解更多信息。 Good luck! 祝好运!

remove this line $scope.dish = dish; 删除这行$scope.dish = dish; then I think it will work.. 那么我认为它会工作..

and assign at the top <div class="container" ng-app="confusionApp"> 并在顶部分配<div class="container" ng-app="confusionApp">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM