[英]angular formly button custom type
我是新來正式的。 我想為按鈕創建自定義模板。 以下代碼片段供參考。
<script type="text/ng-template" id="button.html">
<md-button class={{to.class}} ng-click={{to.method}}>{{to.label}}</md-button>
</script>
問題是在處理ng-click指令時出現了錯誤,因為指令{{to.method}}
尚未被評估。
這樣是不可能的嗎? 有角度的正式示例中沒有一個將按鈕作為模板,從概念上講,這樣做是錯誤的嗎?
編輯:
以下是相應的自定義模板和JSON:
我為按鈕創建了一個自定義模板,如下所示:
<script type="text/ng-template" id="button.html">
<md-button class={{to.class}} ng-click="{{to.method}}">{{to.label}}</md-button>
</script>
相應的JSON為:
{
type: 'button',
templateOptions: {
label: 'Create Item',
class: 'md-raised md-primary',
method: 'createItem'
}
確切的錯誤如下:
錯誤:[$ parse:syntax]語法錯誤:表達式[{{to.method}}]第2列的標記'{'無效鍵,從[{to.method}}開始。
我遇到了類似的問題,這是我為解決該問題所做的工作。 向其他可能有相同問題的人發帖,因為關於按鈕的文檔似乎很少。
注意:角度屬性不需要大括號
設置類型:
formlyConfig.setType({ name: 'button', template: '<button class="{{to.class}}" ng-click="to.method()">{{to.label}}</button>' });
然后將其添加到表單中:
{ key: 'myBtn', type: 'button', templateOptions: { class: 'btn-primary', label: 'My Test Button', method: function() { alert('test'); console.log('test'); } } }
干杯!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.