-
Notifications
You must be signed in to change notification settings - Fork 90
Angular 1.x directive to Angular 1.5 component conversion guide #367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dlabrecq
merged 2 commits into
patternfly:branch-4.0-dev
from
dgutride:convert-to-component-notes
Dec 14, 2016
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Converting Angular 1.4 style directives to Angular 1.5 | ||
|
||
## Directive Conversion | ||
1. Rename file from name-directive.js to name.component.js | ||
2. In the directive - modify the following: | ||
``` | ||
angular.module('patternfly.notification').directive('pfNotificationDrawer', function ($window, $timeout) { | ||
'use strict'; | ||
return { | ||
restrict: 'A', | ||
scope: { | ||
scrollSelector: '@', | ||
groupHeight: '@', | ||
groupClass: '@' | ||
}, | ||
controller: function($window, $timeout) { | ||
$scope.toggle = function () { | ||
<!-------------> | ||
<!-- becomes --> | ||
<!-------------> | ||
angular.module('patternfly.notification').directive('pfNotificationDrawer', { | ||
bindings: { | ||
scrollSelector: '@', | ||
groupHeight: '@', | ||
groupClass: '@' | ||
}, | ||
controller: function ($window, $timeout) { | ||
'use strict'; | ||
var ctrl = this; | ||
|
||
ctrl.toggle = function () { | ||
``` | ||
3. Any initialization logic should be moved out of link functions and into $onInit functions | ||
4. Any event listeners that are added for $window or $timeout events should be cleaned up in $onDestroy | ||
5. $scope watchers should be moved to $onChanges | ||
6. If DOM manipulation still must happen, there is a $postLink function. A bit more investigation will be necessary to see if these components can be upgraded to Angular 2. | ||
|
||
## View Conversion | ||
In the template referenced by the templateUrl in the component, some changes have to be made. Anywhere a former $scope variable is referenced, you'll need to prepend $ctrl | ||
``` | ||
<li ng-repeat="item in items" class="list-group-item" ng-class="{'active': item.isActive}"> | ||
<div ng-if="showBadges && tertiaryItem.badges" class="badge-container-pf"> | ||
``` | ||
Becomes: | ||
``` | ||
<!-- item is in an ng-repeat so no $ctrl is needed --> | ||
<li ng-repeat="item in $ctrl.items" class="list-group-item" ng-class="{'active': item.isActive}"> | ||
<div ng-if="$ctrl.showBadges && tertiaryItem.badges" class="badge-container-pf"> | ||
``` | ||
|
||
## Unit tests | ||
1. Modify test to move attribute directives to component in any html code used in the $compile step. | ||
``` | ||
<div pf-directive></div> | ||
<!-- becomes --> | ||
<pf-directive></pf-directive> | ||
``` | ||
2. Make sure all unit tests pass | ||
|
||
## NgDoc changes | ||
1. Replace the word .directive. with .component. in the ngdoc @name | ||
2. Add @restrict E under the @name | ||
3. In example html, move any attribute directives to component. <div pf-directive></div> becomes <pf-directive></pf-directive> | ||
|
||
## Helpful Links | ||
- https://docs.angularjs.org/guide/component | ||
- https://gist.github.com/toddmotto/5b4de6c777d3e446e6410fdadb824522 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be
.component
rather than.directive
?