【问题标题】:angular expression in html view rendering same data for different expressionshtml视图中的角度表达式为不同的表达式呈现相同的数据
【发布时间】:2017-03-01 17:01:46
【问题描述】:

我的一个视图上的角度表达式正在屏幕上呈现相同的数据

它会显示:你好 - 你好(对于不同的表达式 {{a}}, {{b}})

很明显,控制器中的“a”和“b”变量绑定了不同的数据。

html 视图:

    {{ a | json }} - {{ b | json }}

角度控制器:

app.controller('myctrl', function($scope, myservice) {

      var self = this;

      self.getA = function() {
        $scope.a = 'hello';
      }

      self.getB = function() {
        $scope.b = 'hey';
      }

      self.getA(); //call on controller startup
      self.getB();

    });

【问题讨论】:

  • 它应该可以正常工作,请检查plunker

标签: javascript angularjs model controller


【解决方案1】:

您的代码可以正常运行,

确保您正在执行以下操作。

演示

var app = angular.module('myctrl',[]);
app.controller('myctrl', function($scope) {
      var self = this;
      self.getA = function() {
        $scope.a = 'hello';
      }
      self.getB = function() {
        $scope.b = 'hey';
      }
      self.getA(); //call on controller startup
      self.getB();

});
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Dashboard</title>
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body >
<div ng-app="myctrl" >
<div ng-controller="myctrl as ctrl" >
 {{ a }} - {{ b }}
</div>
</div>
</body>
</html>

【讨论】:

  • 控制器是否应该被注册为一个单独的模块?目前它的 var app = angualr.module("app", []) 然后是 app.controller('ctrl', function(1, 2) {}
  • 它没有单独注册
  • 抱歉,'myctrl' 模块是 ng-app
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-22
  • 1970-01-01
相关资源
最近更新 更多