Last active
November 3, 2016 15:45
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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