好吧,我有一個指令/組件/控制器,即<app-form></app-form>我在多個地方使用這個 html 頁面,但我想從不同的就地組件中洗掉一些特定欄位我該怎么做 angularjs
在 reactjs 中,我們可以這樣做<app-form disableField></app-form>,如果disabledField然后禁用特定欄位,否則什么也不做
再次為了更好地理解,例如,我們有一個表單,其中包含姓名、電子郵件和 dob 相同的表單正在使用多個位置,但是在一個位置我們沒有興趣顯示 dob 我們如何禁用或從特定位置洗掉?
請指導
uj5u.com熱心網友回復:
您必須在組件宣告中使用系結。類似于以下內容:
angular.module('app').component('appForm', {
controller: 'AppFormCtrl',
templateUrl: 'app-form.html',
bindings: {
disableField: "<", // use '<' to generate input on the component
}
});
然后在您的 app-form.html 中,您可以使用 $ctrl 物件訪問輸入變數:
<form>
<input ng-if="$ctrl.disableField == true" type="text"/>
</form>
您可以在根視圖的范圍內傳遞您想要的任何值:
<div>
<!-- displays the form input according to the passed property's value -->
<app-form disable-field="isFieldEnabled"></app-form>
<!-- displays the form input -->
<app-form disable-field="true"></app-form>
<!-- does NOT display the form input -->
<app-form disable-field="false"></app-form>
<!-- does NOT display the form input, as disableField is evaluated an NULL in the component instance -->
<app-form></app-form>
</div>
isFieldEnabled是根控制器 $scope 中的屬性,它將控制組件中輸入欄位的啟用/禁用,但如果不使用邏輯,您可以簡單地傳遞 true 或 false。
您可以附加您想要的任何屬性,它不必是布林值(但在這種情況下,我認為這是有道理的)。
另外,請注意,在 Javascript 環境中定義系結屬性“disableField”時,我們使用camelCase。相同的屬性將使用kebab-case在 vi??ew / html 環境中定義。
您還可以查看以下答案以了解如何從組件實體生成輸出: Angular.js, how to pass value from a component to any other
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/470903.html
標籤:angularjs
