我是一名從事側面項目的Java開發人員。我決定使用AngularJS來使用我的RESTful web服務。AngularJS:具有多個模板的動態自定義指令
我JSON結構是以下:
[
{
"generatorName": "name1",
"constructorParams": [
{
"type": "Boolean",
"value": "true",
},
{
"type": "Integer",
"value": "2",
}
]
},
{
"generatorName": "name2",
"constructorParams": [
{
"type": "Integer",
"value": "10",
},
{
"type": "Integer",
"value": "10",
}
]
}
]
我的目標是顯示基於構造PARAM的「類型」的特定(對前數字,文本等)的輸入字段和初始化它與「價值」。因此,如果選擇了第一個發電機,我想有這樣的事情:
<html>
<select>
<option selected="selected" value="true">Yes</option>
<option value="false">No</option>
</select>
<input type="number" value="2">
<html>
我讀過一些線程,並決定用我環內自定義指令:
<p ng-repeat="param in selectedGenerator.constructorParams">
<constructor-param param="{{param}}"></constructor-param>
</p>
這裏是我的定製指令:
app.directive("constructorParam", function() {
return {
scope : {
param : '@'
},
templateUrl : '/html_templates/constructor_param_template.html'
};
});
而這裏的模板:
<div ng-switch="{{param.type}}">
<div ng-switch-when="Integer">
<input type="number" ng-attr-name="{{param.type}}" min="0" ng-attr-value="{{param.value}}">
</div>
<div ng-switch-when="Boolean">
<select ng-if="{{param.value}}" ng-attr-name="{{param.type}}">
<option selected="selected" value="true">Yes</option>
<option value="false">No</option>
</select>
<select ng-if="!{{param.value}}" ng-attr-name="{{param.type}}">
<option value="true">Yes</option>
<option selected="selected" value="false">No</option>
</select>
</div>
<div ng-switch-when="String">
<input type="text" ng-attr-name="{{param.type}}" ng-attr-value="{{param.value}}">
</div>
<div ng-switch-when="Double">
<input type="number" ng-attr-name="{{param.type}}" step="0.01" min="0" ng-attr-value="{{param.value}}">
</div>
</div>
這些不起作用。我可以在Chrome開發人員的工具中看到該指令已運行,但它不提供任何可見的輸出。我的問題是:
1)我是否正確地在自定義指令元素中傳遞了param對象?
2)我不知道有關指令的scope
- 我也試過param : '=param'
- 這也不行......
3)我應該如何在閱讀傳遞的對象的屬性模板?我試過了:value="{{param.type}}"
,value={{param.type}}
和ng-attr-value="{{param.value}}"
。沒有用,但可能有完全不同的原因...
4)我可以使用前綴「ng-attr-」作爲名稱和值的所有此類元素的HTML屬性嗎?
5)我的模板代碼正是我粘貼的 - 我需要使它成爲一個有效的HTML結構頭部,身體等?我需要將<script>
與AngularJS連接嗎?我已經這樣做了,但是再一次,沒有改變。
6)整個故事的使用場景是從下拉列表中選擇一個具體的生成器,並以指定的方式顯示它的構造器參數。所以它必須通過生成器的更改來重新生成HTML。我假設它是在ng-repeat循環中完成的,但請確認。
非常感謝您的意見! :)