javascript - How to run controller code after scope is compiled -
i have router runs controller when go url: #/create
. have configured so:
$routeprovider .when("/create", { templateurl: "templates/create", resolve: { form: function () { return { fields: [] }; } }, controller: "createcontroller" })
and inside controller load of components asynchroniously inside services , assign respective scope property.
.controller(function ($scope, service /* other components */) { // code console.log($scope); // gives $rootscope service.get().load([["forms"]], $scope); // reaso passing scope service assign loaded data })
sometimes when refresh page have request modules, not have them assigned scope. after lots of debugging have found out problem. running trouble when template not compiled , instead of $scope
getting $rootscope
, modules assigned other/incorrect scope. question here is, how can run controller code after scope compiled?
you should never have $rootscope instead of controller scope. think not configured correctly. should check :
- give name controller (i don't see in example)
- don't have ng-controller on html code call controller
- never try create directly in js code
in hand, should not give $scope service. better use callback instead.
you change controller accordingly :
/* resolve values should inject */ .controller("createcontroller", function ($scope, service, form) { $scope.form = form; service.get().load(function(value) { $scope.somevalues = value; }); })
Comments
Post a Comment