Skip to content

Instantly share code, notes, and snippets.

@jens1101
Last active November 3, 2016 15:45
Show Gist options
  • Select an option

  • Save jens1101/6ee862cb5da3b4c867db2f8816563c9d to your computer and use it in GitHub Desktop.

Select an option

Save jens1101/6ee862cb5da3b4c867db2f8816563c9d to your computer and use it in GitHub Desktop.
Demonstrates how you can use ngModel and ngChange in Angular 1.5 components
angular.module('demo', [])
.component('inputDemo', {
require: {
parent: '^ngModel'
},
template: '<input ng-model="$ctrl.text" /><button ng-click="$ctrl.update()">change</button>',
controller: function($timeout) {
var $ctrl = this;
$ctrl.text;
$ctrl.$onInit = function() {
$timeout(function() {
$ctrl.text = $ctrl.parent.$modelValue;
}, 0);
};
$ctrl.update = function() {
$ctrl.parent.$setViewValue($ctrl.text);
};
}
})
.controller('DemoCtrl', function() {
var $ctrl = this;
$ctrl.foo = 'hello';
$ctrl.func = function(text) {
console.log(text);
}
});
<div ng-app="demo" ng-controller="DemoCtrl as $ctrl">
<input-demo ng-model="$ctrl.foo" ng-change="$ctrl.func($ctrl.foo)"></input-demo>
{{$ctrl.foo}}
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment