Forked from Captain Anonymous's Pen vLQvRx.
Created
February 9, 2016 19:11
-
-
Save masimplo/95822e3566be25394dd3 to your computer and use it in GitHub Desktop.
Default angular
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="app"> | |
| <section ng-controller="MainController as ctrl"> | |
| {{ctrl.hello}} | |
| <input ng-model="ctrl.inputValues" cv-list /> | |
| {{ctrl.inputValues}} | |
| </section> | |
| </div> |
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
| let app = angular.module('app', []); | |
| class MainController { | |
| public hello:string = 'Hello'; | |
| public inputValues = ['hi', 'hey', 'bye']; | |
| } | |
| app.controller('MainController', MainController); | |
| function myDirective(){ | |
| return { | |
| restrict: 'A', | |
| priority: 100, | |
| require: 'ngModel', | |
| link: function(scope, element, attr, ctrl) { | |
| var parse = function(viewValue) { | |
| // If the viewValue is invalid (say required but empty) it will be `undefined` | |
| if (angular.isUndefined(viewValue)) return; | |
| var list = []; | |
| if (viewValue) { | |
| angular.forEach(viewValue.split(','), (value) => { | |
| if (value) list.push(value); | |
| }); | |
| } | |
| return list; | |
| }; | |
| ctrl.$parsers.push(parse); | |
| ctrl.$formatters.push(function(value) { | |
| if (angular.isArray(value)) { | |
| return value.join(','); | |
| } | |
| return undefined; | |
| }); | |
| // Override the standard $isEmpty because an empty array means the input is empty. | |
| ctrl.$isEmpty = function(value) { | |
| return !value || !value.length; | |
| }; | |
| } | |
| }; | |
| } | |
| app.directive('cvList', myDirective); |
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
| <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment