Suppose I have the following component defined:
angular.module('myApp').component('myComponent', {
templateUrl: 'myComponent.html',
bindings: {
myPropOne: '<',
myPropTwo: '<'
}
});
Then, if I instantiate the component in HTML without passing myPropTwo
like this:
<my-component my-prop-one="vm.prop1"></my-component>
Is there a way to make Angular throw an error if myPropTwo
was not passed? Instead of manually checking for it within the controller as shown below:
angular.module('myApp').component('myComponent', {
templateUrl: 'myComponent.html',
bindings: {
myPropOne: '<',
myPropTwo: '<'
},
controller: function() {
this.$onInit = function() {
if(this.myPropTwo === undefined) {
throw new Error('myPropTwo must be passed');
}
}
}
});
In simpler terms, is there a built-in method in Angular to enforce the requirement of passing myPropTwo
when instantiating the component?
Edit:
It seems that @FrankModica's observation holds true. Unfortunately, there might not be a native solution for this. For further clarity, refer to this example on jsFiddle based on @FrankModica's response: https://jsfiddle.net/cafesanu/5L19t3jx/2/