javascript - 在angularjs中支持单向绑定(bind)的指令是什么

标签 javascript angularjs data-binding angularjs-directive angularjs-scope

在 angularjs 中,directives 是什么?支持单向绑定(bind)。 ng-model 支持双向绑定(bind) ng-bind,{{ }} 表达式是否支持单向绑定(bind)?

最佳答案

事实上,当您使用 ng-model 时,AngularJS 默认使用 2 种方式进行数据绑定(bind)。 ng-bind 完全等同于 {{ }},并且它是一种用于在 html 组件中显示值的单向数据绑定(bind)内部 HTML。同样重要的是 ng-bindng-model 一起使用。

为了获得一种数据绑定(bind)方式,您还可以使用 isolated scope 实现自定义指令.在隔离作用域中,有 3 种类型的绑定(bind)选项用作变量的前缀,如下所示:

  • @ 用于文本绑定(bind)
  • & 用于单向绑定(bind)
  • = 用于双向绑定(bind)

在您的 JavaScript 文件中:

angular.module("myApp",[])  
  .directive("myDirective", function () {
    return {
      restrict: "AE", // A refers to a html attribute, E refers to a html element
      scope: {
        text: "@attrText",
        twoWayBind: "=attrTwoWayBind",
        oneWayBind: "&attrOneWayBind"
      }
    };
  }).controller("myController", function ($scope) {
    $scope.info = {name: "dhia", age: 25};
    $scope.text = "Text to be displayed";
});

HTML

<div ng-controller="myController">  
  <div my-directive
    attr-text="{{ text }}"
    attr-two-way-bind="info"
    attr-one-way-bind="text">
  </div>
</div> 

注意:

  • 文本绑定(bind)它们总是字符串
  • 单向绑定(bind)可以是任何类型
  • 双向绑定(bind)可以是任何类型

如果您是 AngularJS directive 的新手,我会推荐去here更好地了解自定义指令的实现方式、不同的指令类型以及属性命名约定。

关于javascript - 在angularjs中支持单向绑定(bind)的指令是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34389843/

相关文章:

javascript - Firefox focus() 设置焦点并立即模糊

javascript - .js 未应用于 ng-view

javascript - 在 angularjs 中定义 ng-minlength 和 ng-pattern 时长度计数错误

javascript - 单击“提交”按钮后刷新/重新渲染 UI

c# - 绑定(bind)到 ItemsControl 的 DataTemplate 内的自定义控件

wpf - 如何在 ListView 控件中设置SelectedItem?

javascript - Angujar JS 表单问题中未定义范围

javascript - BB 代码列表 JavaScript

php - 如何使用ajax添加TinyMCE文本框

javascript - 如何使用 JavaScript 获取服务器日期?