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> 

here's plunk.

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

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -