2017-02-19 112 views
2

我是一名從事側面項目的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循環中完成的,但請確認。

非常感謝您的意見! :)

回答

0

首先,謝謝你們倆的幫助!不幸的是,我不能將任何給定的答案標記爲正確的,因爲我必須將它們混合以獲得我所期望的。

我就不細說了這裏,讓我跟大家分享的最終代碼

<!-- container --> 
<p ng-repeat="param in selectedGenerator.constructorParams"> 
    <constructor-param param="param"></constructor-param> 
</p> 

<!-- template --> 
<div ng-switch="param.type"> 
    <div ng-switch-when="Integer"> 
     <input type="number" name={{param.type}} min="0" value={{param.value}}> 
    </div> 

    <div ng-switch-when="Boolean"> 
     <select ng-if="param.value" name={{param.type}}> 
      <option selected="selected" value="true">Yes</option> 
      <option value="false">No</option> 
     </select> 

     <select ng-if="!param.value" 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" name={{param.type}} value={{param.value}}> 
    </div> 

    <div ng-switch-when="Double"> 
     <input type="number" name={{param.type}} step="0.01" min="0" value={{param.value}}> 
    </div> 
</div> 

而這裏的指令,你都做對:

app.directive("constructorParam", function() { 
    return { 
    scope: { 
     param: '=' 
    }, 
    templateUrl: '/html_templates/constructor_param_template.html' 
    }; 
}); 

再次 - 非常感謝你,如果沒有你的幫助,我不會很快解決這個問題!

1

1)不,你需要傳遞值時卸下花括號指令

<constructor-param param="param"></constructor-param>

2,3)指令中使用=代替@。由於參數的@採取的字符串值,=取參數值

scope: { 
    param: '=' 
}, 

4)如果您需要將數據綁定,然後使用ng-model代替ng-attr

5)你並不需要添加HTML或constructor_param_template.html模板內的身體標記。

6)ng-repeat做到這一點。一旦陣列得到更新,它會動態更新DOM

Demo

2

當你做param : '@'這是一個「文本結合」。如果您想告訴Angular不要將作爲屬性綁定到屬性的內容解釋爲範圍內的屬性,而是直接將其解釋爲字符串,那麼這很有用。

所以,如果你會做

<my-custom-directive param="hello"></my-custom-directive>

然後在你的指令,param就等於字符串"hello"。而如果使用雙向綁定param : '='綁定到param,則Angular會在範圍上查找hello屬性,而不是將其作爲字符串文字。

在你的情況,當你做param="{{param}}" Angular首先通過查看範圍,解壓成一個字符串,然後創建綁定,從而解包「param」。所以即使這可能工作,如果param是一個字符串,在你的情況下,它是一個對象,所以我不認爲它會發揮好。所以我會直接綁定它(請參閱下面的最終代碼)。

在您的指令模板中,您還使用了一些期望綁定作用域的Angular指令,所以我會嘗試綁定而不使用大括號。看到我下面的示例代碼。

app.directive("constructorParam", function() { 
 
    return { 
 
    scope: { 
 
     param: '=' 
 
    }, 
 
    templateUrl: '/html_templates/constructor_param_template.html' 
 
    }; 
 
});
<!-- container --> 
 
<p ng-repeat="param in selectedGenerator.constructorParams"> 
 
    <constructor-param param="{{param}}"></constructor-param> 
 
</p> 
 

 
<!-- TEMPLATE --> 
 

 
<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>

如果還是不行,讓我知道這樣我就可以嘗試在一個CodePen。

相關問題