【问题标题】:what are the directives that support one way binding in angularjs哪些指令支持angularjs中的一种方式绑定
【发布时间】:2016-03-27 04:23:27
【问题描述】:

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

【问题讨论】:

  • Yes..ng-bind & {{}} 做单向绑定,那有什么问题呢?
  • ng-repeat、ng-switch 和 ng-include 默认支持单向绑定,因为它们会创建新的作用域

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


【解决方案1】:

可以通过两种不同的方式指定数据绑定:

带花括号:{{expression}}

使用 ng-bind 指令:ng-bind="varName"

我希望这能回答你的问题。

【讨论】:

    【解决方案2】:

    事实上,当您使用ng-model 时,AngularJS 默认使用 2 路数据绑定。 ng-bind 完全等同于 {{ }}是的,它是一种单向数据绑定,用于将 html 组件内的值显示为 inner HTML。同样重要的是ng-bindng-model 一起使用。

    为了实现单向数据绑定,您还可以使用 isolated scope 实现自定义指令。在隔离范围内,有 3 种类型的绑定选项用作变量的前缀,如下所示:

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

    在您的 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> 
    

    注意:

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

    如果您对AngularJS directive 不熟悉,我建议您转至here 以更好地了解custom 指令是如何实现的以及有哪些不同的 指令类型 以及关于属性命名约定。

    【讨论】:

      【解决方案3】:

      你想如何使用你的范围查看角度站点以了解一种数据绑定方式

      http://www.angularjshub.com/examples/basics/onewaydatabinding/

      <body ng-app ng-init="firstName = 'John'; lastName = 'Doe';">
        <strong>First name:</strong> {{firstName}}<br />
        <strong>Last name:</strong> <span ng-bind="lastName"></span>
      </body>
      

      这是自定义指令

      https://umur.io/angularjs-directives-using-isolated-scope-with-attributes/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-23
        • 2012-10-28
        • 1970-01-01
        • 1970-01-01
        • 2011-07-11
        • 2016-02-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多