javascript - ngInit VS. Firing Controller Function - What are the Downsides? -
i have couple questions regarding use of ng-init - i've see many people online recommend substituting ng-init running desired function controller ready.
what if have 2 vies use same controller, want one of views trigger specific controller function, so
index.html
<div ng-controller="mycontroller"> {{ somestuff }} </div> <div ng-controller="mycontroller" ng-init="run()"> {{ someotherstuff }} </div> app.js
.controller('mycontroller', function($scope){ $scope.somestuff = 'abc'; $scope.run = function(){ $scope.someotherstuff = 'xyz'; } } in scenario, real downside of using ng-init call run()? there issues if run() asynchronous function?
in opinion seems the job fine, may have glossed on why bad idea. input appreciated!
i avoid using ng-init unless there no other ways can solve problem. that's ng-init supposed used for, you'll find plethora of bad usages of ng-init everywhere look.
as per ng-init documentation:
this directive can abused add unnecessary amounts of logic templates. there few appropriate uses of nginit, such aliasing special properties of ngrepeat, seen in demo below; , injecting data via server side scripting. besides these few cases, should use controllers rather nginit initialize values on scope.
a better idea use controller , map templates 1:1 controllers.
Comments
Post a Comment