在 <h3> 标签内显示产品名称

Display the name of the product inside the <h3> tag

本文关键字:产品名 显示 h3 标签      更新时间:2023-09-26
  1. 我正在学习角度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;
  });
})();

你应该使用$scope

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

请尝试以下代码片段。

(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>