Share this
Feature Toggles for Angular UIs
by Adam Knight on 03 October 2017
This article is republished from my original Feature Toggles For Angular UIs post on fantail.io.
I had a chance to demonstrate a feature toggling library to a customer last week, and wanted to share what I did. Feature toggles, in case you don't know, let you to configure how your system behaves without redeploying code. This is fantastic if you have a feature that needs to launch at a particular time, or you want to experiment with by switching off and on in different environments. Feature toggles act like an if statement around a code of block and can be switched on or off as desired, without requiring a restart. Martin Fowler covers this in some detail here.
Filling out my timesheet is one of the least favourite parts of my week, but I thought I would build a simple timesheeting system for my demo. I have a UI written in Angular using the Angular Seed project and the Angular Material components. The basic UI looks like this:
I am working on a couple of features that aren't quite ready yet - being able to add and remove time entries, and to look up projects. To prevent these features launching before I am ready, I'll wrap them with toggles.
My timesheet system will have a couple of REST services (time entries and projects) written in .Net core and Java, and the Angular UI. I need a cross-platform library so I can use the same toggles throughout my application. LaunchDarkly is an excellent commercial library which has clients in JavaScript, Java and .Net (and quite a few more!) so I chose to use it. They offer a 30 day free trial, which is perfect to try out the toggles and see how they fit.
There are many feature toggling libraries available, with open source and commercial options available. I found a good list of options on FeatureFlags.io. Actually, this site is contributed by LaunchDarkly, but also lists other implementations for various languages and frameworks. It also has a lot of useful info on feature toggling, such as best practices and blue/green deployments. All very useful when you're working out how to get started!
First up I added the new features to my UI:
The backend isn't ready to add or delete time entries yet, so I need to toggle these off. I used the JavaScript client and LaunchDarkly's Angular guide for ideas, but made some changes. Feature Toggles work by checking whether a string value is set or not - but I don't want a mis-spelling to prevent a toggle from working or to accidentally forget to remove a toggle when it's finished with. To resolve this I will make use Typescript types:
This lets me use the compiler to make sure the feature toggle exists - I can't mis-spell or forget a toggle now! My LaunchDarkly service works a little differently too, keeping the Flags object private but offering a function to look up a flag's state. It retains the ability to subscribe to state changes though - this is a brilliant feature built into the LaunchDarkly client library that lets the server push out changes to your application.
Now I can use the flags - my toggles are all used in the same component (it's a very simple demo!) but the LaunchDarkly service can be injected into any number of components, and the feature flags watched in the same way:
The Times and Projects services just return a static list of items now - they don't have implementations for Adding or Deleting time entries. I will flesh these out shortly but for now I am leaving them unimplemented.
Putting the feature toggles into the template ends up being really easy. The flags are variables in the component and can be used in the template just like any other properties:
The ngIf directives such as <md-card-actions *ngIf="featureDeleteTimes"> hide the Add and Delete areas if the toggles are off. The result is:
As you can see, the features turn on and off pretty quickly when the dashboard is changed. Being able to toggle this code on and off easily lets me deploy my Angular app with the Delete and Add code in place. I know that when I want to I can flick the toggles, and enable those features. Now I can update my APIs and when they're ready, turn the UI on.
Disclaimer: When I first made this demo, I posted about it on Twitter, and LaunchDarkly kindly offered me some swag - very nice of them. I had already started this post by then, and was planning on using LaunchDarkly for toggles during the free trial period. While it's (very) nice to get swag, I chose this product because of its cross-platform library support, enterprise-grade availability and ease of use.
Adam Knight is an Auckland-based Senior Solution Architect in Equinox IT's Cloud business.
Share this
- Agile Development (153)
- Software Development (126)
- Agile (76)
- Scrum (66)
- Application Lifecycle Management (50)
- Capability Development (47)
- Business Analysis (46)
- DevOps (43)
- IT Professional (42)
- Equinox IT News (41)
- Agile Transformation (38)
- IT Consulting (38)
- Knowledge Sharing (36)
- Lean Software Development (35)
- Requirements (35)
- Strategic Planning (35)
- Solution Architecture (34)
- Digital Disruption (32)
- IT Project (31)
- International Leaders (31)
- Digital Transformation (26)
- Project Management (26)
- Cloud (25)
- Azure DevOps (23)
- Coaching (23)
- IT Governance (23)
- System Performance (23)
- Change Management (20)
- Innovation (20)
- MIT Sloan CISR (15)
- Client Briefing Events (13)
- Architecture (12)
- Working from Home (12)
- IT Services (10)
- Data Visualisation (9)
- Kanban (9)
- People (9)
- Business Architecture (8)
- Communities of Practice (8)
- Continuous Integration (7)
- Business Case (4)
- Enterprise Analysis (4)
- Angular UIs (3)
- Business Rules (3)
- GitHub (3)
- Java Development (3)
- Lean Startup (3)
- Satir Change Model (3)
- API (2)
- Automation (2)
- Scaling (2)
- Security (2)
- Toggles (2)
- .Net Core (1)
- AI (1)
- Diversity (1)
- Testing (1)
- ✨ (1)
- August 2024 (1)
- February 2024 (3)
- January 2024 (1)
- September 2023 (2)
- July 2023 (3)
- August 2022 (4)
- August 2021 (1)
- July 2021 (1)
- June 2021 (1)
- May 2021 (1)
- March 2021 (1)
- February 2021 (2)
- November 2020 (2)
- September 2020 (1)
- July 2020 (1)
- June 2020 (3)
- May 2020 (3)
- April 2020 (2)
- March 2020 (8)
- February 2020 (1)
- November 2019 (1)
- August 2019 (1)
- July 2019 (2)
- June 2019 (2)
- April 2019 (3)
- March 2019 (2)
- February 2019 (1)
- December 2018 (3)
- November 2018 (3)
- October 2018 (3)
- September 2018 (1)
- August 2018 (4)
- July 2018 (5)
- June 2018 (1)
- May 2018 (1)
- April 2018 (5)
- March 2018 (3)
- February 2018 (2)
- January 2018 (2)
- December 2017 (2)
- November 2017 (3)
- October 2017 (4)
- September 2017 (5)
- August 2017 (3)
- July 2017 (3)
- June 2017 (1)
- May 2017 (1)
- March 2017 (1)
- February 2017 (3)
- January 2017 (1)
- November 2016 (1)
- October 2016 (6)
- September 2016 (1)
- August 2016 (5)
- July 2016 (3)
- June 2016 (4)
- May 2016 (7)
- April 2016 (13)
- March 2016 (8)
- February 2016 (8)
- January 2016 (7)
- December 2015 (9)
- November 2015 (12)
- October 2015 (4)
- September 2015 (2)
- August 2015 (3)
- July 2015 (8)
- June 2015 (7)
- April 2015 (2)
- March 2015 (3)
- February 2015 (2)
- December 2014 (4)
- September 2014 (2)
- July 2014 (1)
- June 2014 (2)
- May 2014 (9)
- April 2014 (1)
- March 2014 (2)
- February 2014 (2)
- December 2013 (1)
- November 2013 (2)
- October 2013 (3)
- September 2013 (2)
- August 2013 (6)
- July 2013 (2)
- June 2013 (1)
- May 2013 (4)
- April 2013 (5)
- March 2013 (2)
- February 2013 (2)
- January 2013 (2)
- December 2012 (1)
- November 2012 (1)
- October 2012 (2)
- September 2012 (3)
- August 2012 (3)
- July 2012 (3)
- June 2012 (1)
- May 2012 (1)
- April 2012 (1)
- February 2012 (1)
- December 2011 (4)
- November 2011 (2)
- October 2011 (2)
- September 2011 (4)
- August 2011 (2)
- July 2011 (3)
- June 2011 (4)
- May 2011 (2)
- April 2011 (2)
- March 2011 (3)
- February 2011 (1)
- January 2011 (4)
- December 2010 (2)
- November 2010 (3)
- October 2010 (1)
- September 2010 (1)
- May 2010 (1)
- February 2010 (1)
- July 2009 (1)
- April 2009 (1)
- October 2008 (1)