Search code examples
angularjsangular-ngmodel

Using ng-model with multiple javascript files for one shared html file


I have one html file that has a ng-model binding like this.

<input type="text" ng-model="ClassA.phoneNumber"/>

Can I use another js file to bind with the same element? Something like?

<input type="text" ng-model="ClassA.phoneNumber || ClassB.PhoneNumber"/>

The thing is I don't want to create another HTML file just for binding a simple element. The html file is being reused by multiple js files.


Solution

  • i don't know how your html looks like, but you could do it like this

    <input type="text" ng-if="ClassA.phoneNumber" ng-model="ClassA.phoneNumber" />
    <input type="text" ng-if="ClassB.phoneNumber" ng-model="ClassB.phoneNumber" />
    

    angular will remove the dom element, if the condition is not met