Skip to content

Instantly share code, notes, and snippets.

@kozmic
Created October 22, 2013 13:24
Show Gist options
  • Save kozmic/7100731 to your computer and use it in GitHub Desktop.
Save kozmic/7100731 to your computer and use it in GitHub Desktop.
Angular attribute example
<!doctype html>
<html>
<body>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.3/angular.min.js" ></script>
<script>
var myApp = angular.module('myApp',[]);
myApp.controller('TestCtrl', function($scope) {
$scope.data = {}
});
myApp.directive('nyttelement', function() {
return {
restrict: 'E',
replace: true,
require: 'ngModel',
scope: {
inputname: '@',
modell: '=ngModel'
},
template: '<div><input type="text" data-ng-model="modell" ng-required="true"/></div>',
link: function(scope, iElement, iAttrs) {
iAttrs.$observe('inputname', function(inputnameValue) {
if(inputnameValue) {
iElement.attr('value', inputnameValue);
}
});
}
}
});
</script>
<div ng-app="myApp">
<form name="formName">
<div>Formen sine error: {{ formName.$error }}</div>
<div>Er formen valid: {{ formName.$valid }}</div>
<br/>
<div>Testinput: {{ formName.testinput.$error }}</div>
<div>Testname: {{ formName.testname.$error }}</div>
<div>{'{ inputname }'}: {{ formName['\{\{inputname\}\}'].$error }}</div> <!--- Legges inn som "{{ inputname }}" når den egentlig skal legges inn som "testname" --->
<br/>
<nyttelement data-ng-model="data.modell" inputname="testname">
</nyttelement>
<input data-ng-model="data.test" ng-required="true" type="text" name="testinput"/>
</form>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment