简体   繁体   English

在angularjs中支持单向绑定的指令是什么

[英]what are the directives that support one way binding in angularjs

In angularjs what are the directives that support oneway binding. 在angularjs中,支持单向绑定的指令是什么。 ng-model supports two-way binding Does ng-bind , {{ }} expression supports one way binding ? ng-model支持双向绑定ng-bind{{ }}表达式是否支持单向绑定?

In fact by default AngularJS work with 2 way data-binding when you use ng-model . 实际上,默认情况下,当您使用ng-model时,AngularJS使用2种方式进行数据绑定。 ng-bind is exactly equivalent to {{ }} , and yes it's a one way data binding used for displaying the value inside html component as inner HTML . ng-bind{{ }}完全等效, 是的,它是一种数据绑定方式,用于将html组件的值显示为内部HTML Which is also important is that ng-bind be used along with ng-model . ng-bindng-model一起使用也很重要。

In order to have one way data-binding you can also implement a custom directive with isolated scope . 为了有一种数据绑定的方法,您还可以实现具有隔离 范围的自定义指令。 In isolated scope there are 3 types of binding options used as prefix to your variables as follows: 在隔离范围中,有3种类型的绑定选项用作变量的前缀,如下所示:

  • @ used for Text Binding @用于文本绑定
  • & used for One-way Binding &用于单向绑定
  • = used for Two-way Binding =用于双向绑定

in your JavaScript file: 在您的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 的HTML

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

NB: 注意:

  • Text bindings they are always strings . 文本绑定始终是字符串
  • One-way bindings can be of any type . 单向绑定可以是任何类型
  • Two-way bindings can be of any type . 双向绑定可以是任何类型

If you are kind of new to AngularJS directive , I will recommend to go here to have a better idea how a custom directive is implemented and what are the different directive types and about the attribute naming conventions. 如果您是AngularJS指令的新手,我建议您去这里有一个更好的主意,如何实现自定义指令,什么是不同的指令类型以及有关属性命名约定。

A data binding can be specified in two different ways: 可以通过两种不同的方式指定数据绑定:

with curly braces: {{expression}} 大括号: {{expression}}

with the ng-bind directive: ng-bind="varName" 使用ng-bind指令: ng-bind="varName"

I hope that answers your question. 我希望能回答您的问题。

It is on you how you want to use your scope see angular site for one way data bind 这是您要如何使用示波器的一种方法,请参见角度站点以一种方式绑定数据

http://www.angularjshub.com/examples/basics/onewaydatabinding/ 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> 

and this is for custom directive 这是用于自定义指令

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM