问题
I have a sample of icons in a HBox
in an XML view as below:
<m:HBox>
<core:Icon src="sap-icon://show" color="#007bff" id="test1" press="onPressView" />
<core:Icon src="sap-icon://edit" color="#007bff" id="test2" press="onPressEdit" />
<core:Icon src="sap-icon://print" color="#007bff" id="test3" press="onPressPrint" />
</m:HBox>
In controller to set visible false
, I am doing as:
let testIcon1 = this.getview().byId('test1');
testIcon1.setVisible(false);
But this isn't working.
回答1:
You're probably accessing a template control instead of the actual rendered one. If you inspect the element in the devtools, see if the ID of the rendered control (Icon) has the suffix __clone<n>
.
Since such ID suffixes are generated by the framework, accessing cloned controls via byId
should be avoided. There are many alternative solutions in such cases. A general good solution is to separate views from the controller by binding the control properties (e.g. visible
) and manipulating those properties from the model.
来源:https://stackoverflow.com/questions/61674679/manipulating-control-accessed-with-byid-has-no-effect