javascript - ng-init vs function in controller, which is better practice when using $parent? -
i have bootstrap modal corresponds ng-repeat list. access ng-repeat scope data parent, contains modal. when modal button clicked on list, corresponding data json appears in modal.
i have found 2 ways of doing this, , wonder best alternative?
method 1 view:
<li ng-init="myfunction(item,$parent)" ng-repeat="item in data.webapps_1.items>
controller:
$scope.myfunction = function(item,parent){ parent.selected=item.counter-1; };
method 2 view:
<li ng-init="$parent.selected=item.counter-1" ng-repeat="item in data.webapps_1.items>
with nothing in controller.
i have read in angular nginit docs
the appropriate use of nginit aliasing special properties of ngrepeat, seen in demo below. besides case, should use controllers rather nginit initialize values on scope.
but list of special properties of ngrepeat not include $parent.
so, better practice? including expression $parent.selected=item.counter-1
in controller or in nginit directive?
thanks in advance
either of 2 fine really, long you're consistent. depends on scale of app though.
imo if app going large you'll want go function way, better adhere whole mvc philosophy of decoupling , separation of concerns (http://victorblog.com/2013/03/18/angularjs-separation-of-concerns/).
ng-init="myfunction(item,$parent)"
it's better structure because want keep of business logic in javascript controllers, not in view.
Comments
Post a Comment