Ajax 板


LINE

※ 引述《mrbigmouth (大嘴先生)》之铭言: : 但html是一个很好的"资料表达"语言,而不是一个好的"程式"语言 这句话说的好,不过 XML 本来就是种资料表示法吧? 我觉得 Angular 的 Directive 从根本上来说就不是 Template engine。 Server 端的许多 Template engine,核心都是 Text processing + Binding。 而 Angular 只有 Binding,没有 Text processing。 Directive 的功用为扩充 HTML,为其增加新的行为。 : <header ng-repeat-start="item in items"> : Header {{ item }} : </header> : <div class="body"> : Body {{ item }} : </div> : <footer ng-repeat-end> : Footer {{ item }} : </footer> 这个是在 Angular 中违和感最重的一个 directive 了。跟其它的 directive 格格不入,我也觉得很微妙。 一般 ng-repeat 的行为︰重覆自身,并把资料丢进 child scope 内。 <section ng-repeat="item in items"> ... </section> 这样写不是很好吗 : <% : for (item in items) { : %> : <header> : Header <%- item %> : </header> : <div class="body"> : Body <%- item %> : </div> : <footer> : Footer <%- item %> : </footer> : <% : } : %> 这个就回归一般的 text processing。事实上用它来 render 非 HTML 的东西 也没什麽不可。 : <div ng-show="someBooleanVariable"></div> ng-show 的行为︰当资料为 no、false 时,隐藏自己 : <% : if (someBooleanVariable) { : %> : <div></div> : <% : } : %> 这也回归一般的 text preprocessing。 Directive 和这些 template engine 的不同,就在於 directive 是用来改变 已经 render 出来,HTML element 的行为。而 template engine 是根据定好 的流程,把 HTML render 出来。 以我来说,Directive 的做法是比较适合前端的,而一般的 template engine 适合後端。 要是想做 text processing,也可以把 template engine 写成 directive <div template-engine template-engine-data="myTemplate"> <% if (var) { %> <span>Hi</span> <% } %> </div> template-engine 行为︰将自身内容和 templateEngineData 丢给 text processor,再将结果替换为自身内容。还可以 watch templateEngineData,当资料改变时 re-render……等等。 说不定已经有人实作过了。 -- (* ̄▽ ̄)/‧★*"`'*-.,_,.-*'`"*-.,_☆,.-*` http://i.imgur.com/oAd97.png --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.25.222.65
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/Ajax/M.1416081186.A.110.html ※ 编辑: eight0 (114.25.222.65), 11/16/2014 03:55:00
1F:→ carylorrk: 不用这麽麻烦吧?如果你爽可以先让 string based 的 11/16 10:35
2F:→ carylorrk: template 跑过一遍啊XD 11/16 10:35
3F:推 mrbigmouth: 双向绑定的优点不就是图省事吗....你这样做好像没省到 11/16 14:12
4F:→ mrbigmouth: 事? 那干嘛不用js自己干就好? 11/16 14:13
5F:→ mrbigmouth: 说真的 watch/re-render直接用js硬干是会花多大的功 11/16 14:14
6F:→ mrbigmouth: 夫呢...~"~ 为什麽非把变数判断写在html上呢.... 11/16 14:14
7F:→ eight0: @cary 所以我觉得後端比较适合跑一般的 template 11/16 15:43
8F:→ eight0: @mrbm 那只是设想说如果要在 Angular 内实作可以怎麽做啦 11/16 15:44
9F:→ eight0: 另外写成 directive,其它的 Angular 专案也可以用。 11/16 15:45
10F:→ eight0: 不过我还是不认为 directive 内的东西是「变数判断」 11/16 15:46
11F:→ superpai: @mrbigmouth 你的「为什麽」的答案你自己就说过了 11/16 15:56
12F:→ superpai: 「html是一个很好的"资料表达"语言,而不是一个好的..」 11/16 15:57
13F:→ superpai: 只能说有的人的想法就是这东西不好我就不要用 11/16 15:57
14F:→ superpai: 有的人觉得不够好我们来改进他 11/16 15:57
15F:推 tomap41017: 写React吧! 11/17 20:35







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Gossiping站内搜寻

TOP