I have seen some directive using question mark ('?') in the scope properties definition like so:
app.directive('userInfo', function() {
return {
restrict: 'A',
scope: {prop: '=?'},
templateUrl: 'some/template/url',
};
I have looked for it and the only think i saw was this : what's the meaning of '=?' in angularJS directive isolate scope declaration?. But it's not working the way he explains it there. I have succeeded running my application and no Exception was thrown.
Can somebody elaborate more about it?
In some code snippets, you may have seen that Angular expressions have a question mark after them. Angular refers to it as ‘Safe Navigation Operator’ .It makes sure that we are not seeing null and undefined values in our application when we want to access properties of an object.
AngularJS comes with a set of these directives built-in, like ngBind, ngModel, and ngClass. Much like you create controllers and services, you can create your own directives for AngularJS to use. When AngularJS bootstraps your application, the HTML compiler traverses the DOM matching directives against the DOM elements.
science portal for geeks. The ng-click Directive in AngluarJS is used to apply custom behavior when an element is clicked. It can be used to show/hide some element or it can popup alert when button is clicked. Example: This example uses ng-click Directive to display an alert message after clicking the element.
Example: This example uses ng-app Directive to define a default AngularJS application. The ng-init directive is used to initialize an AngularJS Application data. It defines the initial value for an AngularJS application and assigns values to the variables. The ng-init directive defines initial values and variables for an AngularJS application.
If you don't add the ?
and don't put a prop
property on your element you're using the directive on, then an exception will be thrown.
Adding the ?
, marks the property as optional. As mentioned in the documentation for $compile
:
You can avoid this behavior using =? or =?attr in order to flag the property as optional.
I think the documentation might be a bit outdated in that area. NON_ASSIGNABLE_MODEL_EXPRESSION
only appears in older revisions of the source.
Please note that the exception is only thrown once you try to write to the scope property. I threw together a quick plunkr to showcase the issue: http://plnkr.co/edit/hjUq6ZisuRG2C3mZpRDj?p=preview
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With