ng-model和ng-bind之间有什么区别

I'm currently learning AngularJS and am having difficulty understanding the difference between ng-b
摘要由CSDN通过智能技术生成

本文翻译自:What's the difference between ng-model and ng-bind

I'm currently learning AngularJS and am having difficulty understanding the difference between ng-bind and ng-model . 我目前正在学习AngularJS,并且很难理解ng-bindng-model之间的区别。

Can anyone tell me how they differ and when one should be used over the other? 任何人都可以告诉我他们的差异,以及何时应该使用另一个?


#1楼

参考:https://stackoom.com/question/q6uR/ng-model和ng-bind之间有什么区别


#2楼

ng-bind has one-way data binding ($scope --> view). ng-bind具有单向数据绑定($ scope - > view)。 It has a shortcut { { val }} which displays the scope value $scope.val inserted into html where val is a variable name. 它有一个快捷方式{ { val }} ,它显示插入到html中的范围值$scope.val ,其中val是一个变量名。

ng-model is intended to be put inside of form elements and has two-way data binding ($scope --> view and view --> $scope) eg <input ng-model="val"/> . ng-model旨在放在表单元素内,并具有双向数据绑定($ scope - > view和view - > $ scope),例如<input ng-model="val"/>


#3楼

tosh 's answer gets to the heart of the question nicely. 托什的回答很好地解决了问题的核心。 Here's some additional information.... 这是一些额外的信息....

Filters & Formatters 过滤器和格式化程序

ng-bind and ng-model both have the concept of transforming data before outputting it for the user. ng-bindng-model都具有在为用户输出数据之前转换数据的概念。 To that end, ng-bind uses filters , while ng-model uses formatters . 为此, ng-bind使用过滤器 ,而ng-model使用格式化程序

filter (ng-bind) 过滤器(ng-bind)

With ng-bind , you can use a filter to transform your data. 使用ng-bind ,您可以使用过滤器来转换数据。 For example, 例如,

<div ng-bind="mystring | uppercase"></div> , <div ng-bind="mystring | uppercase"></div>

or more simply: 或更简单地说:

<div>{ {mystring | uppercase}}</div>

Note that uppercase is a built-in angular filter , although you can also

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值