在 angularjs 中,directives 是什么?支持单向绑定(bind)。 ng-model
支持双向绑定(bind)
ng-bind
,{{ }}
表达式是否支持单向绑定(bind)?
最佳答案
事实上,当您使用 ng-model
时,AngularJS 默认使用 2 种方式进行数据绑定(bind)。 ng-bind
完全等同于 {{ }}
,并且是它是一种用于在 html 组件中显示值的单向数据绑定(bind)内部 HTML。同样重要的是 ng-bind
与 ng-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/