angularjs - Can't access $http data in angular in bootstrap modal -


i have used search function, didn't find solution solves problem. i'm having ui bootstrap directives in angular. before opening modal, want make $hhtp call data modal. works perfectly, except data not present in first opening modal. when click again data present.

here's code:

myapp.controller( 'jobscontroller', [ '$rootscope', '$scope', '$http', '$modal', function( $rootscope, $scope, $http, $modal ) {        $scope.post = [];        $scope.getjobs = function(id) {            $http({              method: 'get',              url: $scope.api,              params: {                  'type' : 'jobs',                  'filter[p]' : id,                  'filter[post_status]' : 'publish'              }          }).          success( function( data ) {              $scope.post = data[0];          });        };        $scope.open = function(id) {            $scope.getjobs(id);            $scope.opts = {              backdrop: true,              keyboard: true,              backdropclick: true,              scope: (function () {                  var scope = $rootscope.$new();                  scope.post = $scope.post;                  return scope;              })(),              templateurl: 'mymodalcontent.html',              controller: 'modalinstancectrl'          };                  $modal.open($scope.opts);      };    }]);    myapp.controller('modalinstancectrl', ['$scope', '$modalinstance', function ($scope, $modalinstance) {      $scope.cancel = function () {          $modalinstance.dismiss('cancel');      };  }]);

i have tried several tutorials , code snippets site , others, did not find solution. appreciated.

thx lot

the $http.get async call. call needs "come back" before accessing data.

$scope.getjobs = function(id) {      return $http({         method: 'get',         url: $scope.api,         params: {             'type' : 'jobs',             'filter[p]' : id,             'filter[post_status]' : 'publish'         }     });  };  $scope.open = function(id) {      $scope.getjobs(id).     then( function( data ) {         $scope.post = data[0];          $scope.opts = {           backdrop: true,           keyboard: true,           backdropclick: true,           scope: (function () {             var scope = $rootscope.$new();             scope.post = $scope.post;             return scope;           })(),           templateurl: 'mymodalcontent.html',           controller: 'modalinstancectrl'         };          $modal.open($scope.opts);      }); }; 

so in order make work, easiest way deal resolve of promise inside open function.


ps : code works, not angular-styled, nor efficient.

  • you don't need put of objects inside $scope. can create local variables if need them inside controller.
    eg :

    $scope.opts = {};

can replace :

var opts = {}; 

this prevent angular checking value of variables everytime $digest()

  • delegate server calls service. able share calls between controllers.

  • if possible use controller as syntax.

do not hesitate read styleguide : https://github.com/johnpapa/angular-styleguide


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -