【问题标题】:How to listen to model in angularjs using controller as syntax?如何使用控制器作为语法在 angularjs 中收听模型?
【发布时间】:2014-04-02 08:31:02
【问题描述】:

我不知道如何通过 $scope 监听控制器的属性

http://codepen.io/Tek/pen/fgIkG/ - 控制器作为语法

http://codepen.io/Tek/pen/zqCpF /?editors=111 - $scope

【问题讨论】:

  • 在此处粘贴一些代码。此外,请清楚您面临的问题以及您期望的解决方案。输入尽可能多的细节。我不确定它是否只适合我,但我似乎无法访问链接

标签: javascript angularjs


【解决方案1】:

您需要在控制器内使用$scope 而不是this 来绑定值。

Here's the edited code 我相信你会选择this

【讨论】:

  • 在你的例子中“控制器作为语法” - 变得没用
【解决方案2】:

在最新版本的Angular JS1.2 中,他们引入了一个新关键字controllerAs 以使控制器内部没有范围成为可能。

<div ng-controller="testCtrl as test">
    {{test.value}}
</div>

在你的控制器中

app.controller('testCtrl ', function () {
   this.value = 'Hello World';
});

看到上面的控制器是在没有注入$scope的情况下生成的。

Here 是一个很好的视频教程来解释这一点

【讨论】:

  • 我知道,但是如何监听控制器范围内的变化?
猜你喜欢
  • 2015-12-20
  • 1970-01-01
  • 2014-07-27
  • 2014-02-12
  • 1970-01-01
  • 2020-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多