2016-09-16 72 views
0

檢查我的代碼:NG-INIT不AngulaJS工作

JSON

var json = '[{"type":"product","id":1,"label":"Size","isRequired":true,"errorMessage":"","enablePrice":true,"description":"","placeholder":"Select Size","defaultValue":"","choices":[{"text":"Size 30","value":"Size 30","isSelected":false,"price":"$100.00"},{"text":"Size 32","value":"Size 32","isSelected":false,"price":"$100.00"},{"text":"Size 34","value":"Size 34","isSelected":false,"price":"$100.00"},{"text":"Size 36","value":"Size 36","isSelected":false,"price":"$100.00"}],"conditionalLogic":"","productField":"","basePrice":"$0.00","pageNumber":1},{"type":"option","id":2,"label":"Select Colors","isRequired":true,"errorMessage":"","inputType":"select","choices":[{"text":"Denim","value":"Denim","isSelected":false,"price":"$0.00"},{"text":"Black","value":"Black","isSelected":false,"price":"$0.00"},{"text":"Brown","value":"Brown","isSelected":false,"price":"$0.00"}],"enablePrice":true,"productField":1,"formId":24,"description":"","allowsPrepopulate":false,"inputMask":false,"inputMaskValue":"","labelPlacement":"","descriptionPlacement":"","subLabelPlacement":"","placeholder":"Select Jean Color","cssClass":"","inputName":"","adminOnly":false,"noDuplicates":false,"defaultValue":"","inputs":"","conditionalLogic":{"actionType":"show","logicType":"any","rules":[{"fieldId":"1","operator":"is","value":"Size 30"},{"fieldId":"3","operator":"is","value":"Regular"}]},"failed_validation":"","multipleFiles":false,"maxFiles":"","calculationFormula":"","calculationRounding":"","enableCalculation":"","disableQuantity":false,"displayAllCategories":false,"useRichTextEditor":false,"pageNumber":1,"displayOnly":""},{"type":"option","id":3,"label":"Select Fit","adminLabel":"","isRequired":true,"size":"medium","errorMessage":"","inputType":"select","choices":[{"text":"Regular","value":"Regular","isSelected":false,"price":"$0.00"},{"text":"Skinny","value":"Skinny","isSelected":false,"price":"$10.00"},{"text":"Fit","value":"Fit","isSelected":false,"price":"$5.00"}],"enablePrice":true,"productField":1,"formId":24,"description":"","allowsPrepopulate":false,"inputMask":false,"inputMaskValue":"","labelPlacement":"","descriptionPlacement":"","subLabelPlacement":"","placeholder":"Select Jean Fit","cssClass":"","inputName":"","adminOnly":false,"noDuplicates":false,"defaultValue":"","inputs":"","conditionalLogic":{"actionType":"show","logicType":"any","rules":[{"fieldId":"1","operator":"is","value":"Size 30"},{"fieldId":"1","operator":"is","value":"Size 32"},{"fieldId":"1","operator":"is","value":"Size 34"},{"fieldId":"1","operator":"is","value":"Size 36"}]},"failed_validation":"","multipleFiles":false,"maxFiles":"","calculationFormula":"","calculationRounding":"","enableCalculation":"","disableQuantity":false,"displayAllCategories":false,"useRichTextEditor":false,"enableChoiceValue":false,"pageNumber":1,"displayOnly":""},{"type":"option","id":4,"label":"Select Rise","adminLabel":"","isRequired":true,"size":"medium","errorMessage":"","inputType":"select","choices":[{"text":"High","value":"High","isSelected":false,"price":"$29.00"},{"text":"Low","value":"Low","isSelected":false,"price":"$0.00"}],"enablePrice":true,"productField":1,"formId":24,"description":"","allowsPrepopulate":false,"inputMask":false,"inputMaskValue":"","labelPlacement":"","descriptionPlacement":"","subLabelPlacement":"","placeholder":"Select Rise","cssClass":"","inputName":"","adminOnly":false,"noDuplicates":false,"defaultValue":"","inputs":"","conditionalLogic":{"actionType":"show","logicType":"any","rules":[{"fieldId":"1","operator":"is","value":"Size 30"},{"fieldId":"1","operator":"is","value":"Size 32"},{"fieldId":"1","operator":"is","value":"Size 34"},{"fieldId":"1","operator":"is","value":"Size 36"}]},"failed_validation":"","multipleFiles":false,"maxFiles":"","calculationFormula":"","calculationRounding":"","enableCalculation":"","disableQuantity":false,"displayAllCategories":false,"useRichTextEditor":false,"enableChoiceValue":false,"pageNumber":1,"displayOnly":""},{"type":"total","id":5,"label":"Total","adminLabel":"Total ","isRequired":false,"size":"medium","errorMessage":"","inputs":null,"formId":24,"description":"","allowsPrepopulate":false,"inputMask":false,"inputMaskValue":"","inputType":"","labelPlacement":"","descriptionPlacement":"","subLabelPlacement":"","placeholder":"","cssClass":"","inputName":"","adminOnly":false,"noDuplicates":false,"defaultValue":"","choices":"","conditionalLogic":{"actionType":"show","logicType":"any","rules":[{"fieldId":"1","operator":"is","value":"Size 30"},{"fieldId":"1","operator":"is","value":"Size 32"},{"fieldId":"1","operator":"is","value":"Size 34"},{"fieldId":"1","operator":"is","value":"Size 36"}]},"failed_validation":"","productField":"","multipleFiles":false,"maxFiles":"","calculationFormula":"","calculationRounding":"","enableCalculation":"","disableQuantity":false,"displayAllCategories":false,"useRichTextEditor":false,"pageNumber":1,"displayOnly":""}]'; 
$scope.attributes = json; 

HTML

<div ng-repeat="conditions in attributes.conditionalLogic.rules"> 
    <select class="form-control" ng-init="option.id = conditions.fieldId" name="conditon_dropdown" ng-model="conditions.fieldId" ng-change="" ng-options="option.id as option.placeholder for option in attributes"></select> 
</div> 

輸出

<select ng-options="option.id as option.placeholder for option in attributes" ng-change="" ng-model="conditions.fieldId" name="conditon_dropdown" ng-init="option.id = conditions.fieldId" ng-selected="conditions.fieldId" class="form-control ng-pristine ng-valid ng-not-empty ng-touched"> 
<option value="?" selected="selected"></option> 
<option label="Select Size" value="number:1">Select Size</option> 
<option label="Select Jean Color" value="number:2">Select Jean Color</option> 
<option label="Select Jean Fit" value="number:3">Select Jean Fit</option> 
<option label="Select Rise" value="number:4">Select Rise</option> 
</select> 

我曾經嘗試都ng-init來選擇我的默認選項。但在我的下拉列表中沒有填充。請幫幫我。

+0

發佈您的json,您從中填充下拉菜單 – Sajeetharan

+0

當頁面加載時,'conditions'變量可能沒有值。僅當編譯放置它的DOM時調用'ng-init' .. –

+0

@PankajParkar 'conditions.fieldId'我得到'1' – Chinmay235

回答

0

您必須初始化fieldId像原樣

$scope.fieldId='1'; 

字符串,因爲angularjs在ng-model在下拉的情況下申請根據您的要求,需要字符串值,你必須做出conditions.filedId使用ng-init等作爲字符串 -

<div ng-repeat="conditions in attributes.conditionalLogic.rules"> 
    <select class="form-control" ng-init="conditions.fieldId=conditions.fieldId.toString()" name="conditon_dropdown" ng-model="conditions.fieldId" ng-change="" ng-options="option.id as option.placeholder for option in attributes"></select> 
</div> 

我希望這會對你有幫助。

+0

'conditions.fieldId = conditions.fieldId.toString()'這裏這個條件我來了false。 – Chinmay235

+0

@chinu,過來錯誤??我沒有得到你的觀點,在這裏,我只是初始化conditions.fieldId到字符串 –

+0

意味着我的當前值沒有被選中 – Chinmay235