我在開發我的Angular庫時遇到了一個問題:angular-coq。
我想提供這些功能:
<div ng-controller="myController">
<form coq-model="team1">
<!-- Inputs will be automatically added to form (wrapped in <p>) -->
</form>
</div>
隨着myController
,在範圍team1
揭露,有2個屬性的自定義類:id
,name
。
CoqModel
指令將爲每個屬性附加元素a <input coq-model-attribute="...">
。
CoqModelAttribute
指令需要一個CoqModel
父有電流模式(這裏team1
)的參考,並編譯如下:
<div ng-controller="myController">
<form coq-model="team1">
<input type="<attribute_name>" ng-model="team1.<attribute_name>" coq-model-attribute="<attribute_name>"/>
</form>
</div>
Here is the implementation of CoqModel directive和here is the implementation of CoqModelAttribute directive(太大,貼在這裏)
你會看到我「雙 - $編譯」CoqModel
指令。 如果我不這樣做,生成的輸入ng-model
將不會被綁定..
因此,一切工作正常,期望「雙$編譯」導致ng-click
一些問題。
在這種情況下:
<div ng-controller="myController">
<form coq-model="team1">
<input coq-model-attribute="name"/>
<br />
<input type="button" value="click me" ng-click="team1.update()" />
</form>
</div>
點擊「點擊我」將調用方法team1.update
2倍..
我已經嘗試了很多不同的重構,都未能解決這個問題... 如果你們其中一位是角度專家,請告訴我如何解決這個問題:)
非常感謝!
你在做什麼,你首先需要手動調用'$ compile'?它看起來像你的'link'函數中的大部分邏輯產生的標記不應該在這裏完成。你應該爲你的指令使用一個模板,並用它來創建你的標記。即使您真的需要生成標記,它應該在您的指令的「編譯」功能內完成,而不是「鏈接」。看看ngRepeat的這個解釋,例如:http://liamkaufman.com/blog/2013/05/13/understanding-angularjs-directives-part1-ng-repeat-and-compile/ –
你好@ju 感謝你的快速回復。 我無法在編譯函數中生成標記,因爲我需要$ parse attributes value來知道要創建多少個節點。 而$ parse需要範圍來評估值......你看到問題了嗎? – wittydeveloper