Skip to content

Instantly share code, notes, and snippets.

@stuf
Last active August 29, 2015 14:15
Show Gist options
  • Save stuf/0cefc736248409ce5bae to your computer and use it in GitHub Desktop.
Save stuf/0cefc736248409ce5bae to your computer and use it in GitHub Desktop.

A shim for your pleasure, so that in case you have to attach Angular model directives to form fields (without clearing the initial value) that are also being used in .NET WebForms.

angular.module('MyApp', [])
	.run(function () {
		 var d = document.querySelectorAll('input[type="text"]');
		 for (var i = d.length; i-- > 0;) {
		 	var e = d[i];
			if (e.hasAttribute('ng-model') && e.value != null && e.value != 0) {
				var model = e.getAttribute('ng-model');
				if (!(model === 'selectedProduct' || model === 'selectedVariation')) {
					e.setAttribute('ng-init', (e.getAttribute('ng-model') + ' = "' + e.value + '"'));
				}
			}
		 }
	});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment