I need to dynamically assign an ng-model variable based on a condition. For instance:
<input type="text" ng-model="item.model[multilang]" >
The $scope.multilang
variable can be set to either "ENG"
, "JP"
(languages) or false
. So, when multilang = "ENG"
and the user types in "Hello"
into the input field, the result will be:
item.model = {ENG: "Hello"}
The issue arises when $scope.multilang
is set to false
. In such cases, I want the result to simply be:
item.model = "Hello"
I am struggling to figure out how to achieve this scenario. One potential solution that comes to mind is to change the ng-model attribute based on the value of $scope.multilang
- so if it's false, the ng-model would become ng-model="item.model"
. However, I'm not sure how to implement this.
EDITED I have come up with one possible solution:
<input ng-if="multilang" type="text" ng-model="item.model[multilang]" >
<input ng-if="!multilang" type="text" ng-model="item.model" >
Is there a better way to achieve this functionality?