【问题标题】:Display the name of the product inside the <h3> tag在 <h3> 标签内显示产品名称
【发布时间】:2015-01-22 17:44:59
【问题描述】:
  1. 我正在学习 Angular js

  2. 我正在做一些小任务..

  3. 但我收到此错误 显示里面的产品名称 标签。

  4. 你能告诉我如何解决它

  5. 在下面提供我的代码

html

<!DOCTYPE html>
<html ng-app="gemStore">
  <head>
    <link rel="stylesheet" type="text/css" href="bootstrap.min.css" />
    <script type="text/javascript" src="angular.min.js"></script>
    <script type="text/javascript" src="app.js"></script>
  </head>
  <body ng-controller="StoreController as store">
    <div class="product row">
      <h3>
        {{store.product.name}}
        <em class="pull-right"></em>
      </h3>
    </div>
  </body>
</html>

js

(function(){
  var gem = { name: 'Azurite', price: 2.95 };
  var app = angular.module('gemStore', []);
  app.controller('StoreController',function(){
    this.product = gem;
  });
})();

【问题讨论】:

  • 这是代码学校吗?
  • @SandroEric 我只是在学习...
  • 对不起。我没有冒犯的意思。我只是问你是否正在使用 codeschool.com 材料?
  • @mapuut 你必须在使用 ng-app 之前加载所有 angularjs 脚本
  • 你的代码运行良好,这就是它的小提琴jsfiddle.net/w2fzfuho

标签: javascript jquery angularjs backbone.js


【解决方案1】:

你应该使用$scope:

app.controller('StoreController',function($scope){
    $scope.product = gem;
});

{{product.name}}

请尝试以下sn-p。

(function(){
  var gem = { name: 'Azurite', price: 2.95 };
  var app = angular.module('gemStore', []);
  app.controller('StoreController',function($scope){
    $scope.product = gem;
  });
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app="gemStore">
  <head>
    <link rel="stylesheet" type="text/css" href="bootstrap.min.css" />
    <script type="text/javascript" src="angular.min.js"></script>
    <script type="text/javascript" src="app.js"></script>
  </head>
  <body ng-controller="StoreController">
    <div class="product row">
      <h3>
        {{product.name}}
        <em class="pull-right"></em>
      </h3>
    </div>
  </body>
</html>

【讨论】:

    猜你喜欢
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 2016-04-24
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多