javascript - AngularJS Set Value to $scope from Nested Function -
i'm trying value function inside function:
controller
$scope.vm = {}; function myfunc(){ $scope.vm.hello = 'hello'; function myfunction(){ $scope.vm.world = 'world'; } } myfunc();
view
<p>{{vm.hello}} {{vm.world}}</p>
how can display "hello world"?
i assume trying achieve called 'closure'. if so, modify controller to:
app.controller('mainctrl', function($scope) { $scope.vm = {}; function myfunc(){ $scope.vm.hello = 'hello'; return function () { $scope.vm.world = 'world'; } } var hello = myfunc(), // invokes outer function world = hello(); // invokes inner function console.log($scope.vm); });
in code, inner function myfunction()
cannot called outside myfunc()
method, because scope bounded outer method. can of course call directly inside outer method, or better - make inner function immediate:
function myfunc(){ $scope.vm.hello = 'hello'; (function myfunction(){ $scope.vm.world = 'world'; })(); }
Comments
Post a Comment