user3152131 user3152131 - 1 year ago 56
Javascript Question

Is it wrong to define controller in angular wrapped in a function?

I am looking at some code, and I see that it is written as shown below. It doesn't make sense to me. Is it wrong? Why is it written like that? Also, shouldn't the

use strict;
go at the very top, outside of the code?

(function() {
'use strict';
angular.module('itemList', [])
.component('itemList', {
templateUrl: 'item-list/item-list.component.html',
controller: ['Item', ItemController]

function ItemController(Item) {

Answer Source

The main reason it is wrapped in an IIFE is to keep "use strict" local to the code contained within.

Otherwise if "use strict" was set global it would possibly affect other unrelated scripts that my not comply and thus would cause unexpected errors to be thrown