html - 克隆按钮的 Angular 方式是什么?

标签 html css angularjs state modular

我有一个针对特定用户的关注按钮,点击后应将其文本更改为已关注,反之亦然。此关注按钮可以显示在页面上的不同模块中。单击后,此特定用户的关注按钮应在所有这些模块中更新。但是,这些按钮在不同的范围内。确保克隆按钮处于相同状态的 Angular 方式是什么?

我目前的解决方案是使用通用的 jQuery 选择器来更新点击时的所有按钮。

最佳答案

您应该将状态存储在服务中。

例子:

app.factory('SharedService', function() {
  this.buttonState = null;

  this.setButtonState= function(value) {
    this.buttonState = value;
  }

  this.getButtonState= function() {
    return this.buttonState ;
  }

  return this;
});

阅读:AngularJS Docs on services

或检查此 Egghead.io video

关于html - 克隆按钮的 Angular 方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30405820/

相关文章:

html - 将图像对齐到段落 css 的右侧

iphone - 从HTML文件中提取所有<script>标记并将其存储在NSArray-Objective C中

javascript - 并排放置 2 个对象,其中 1 个在 css 中具有固定大小

css - 如何使 bootbox.prompt 工作?

javascript - 复制 $scope 持有的数据

javascript - AngularJS 工厂 : Best way to call parent in literal?

javascript - 按顺序加载网页内容

php - 正则表达式忽略 HTML 标签中已有的 URL

javascript - 检查javascript和警报中的背景颜色

javascript - angular-animate.js 中的奇怪错误