2
votes

Could you please let me know what is wrong with my code? I get the initial HTML page, but when I click on "Open", nothing happens. Not even the console logs an error, or any other change.

app.js

var app = angular.module('carApp', ['ui.bootstrap']);

ctrl.js

app.controller('carCtrl', function($scope, $http, $uibModal) {
    $http.get('jobs.json').success(function(data) {
        $scope.data = data;

        $scope.open = function() {

            var modalContent = $uibModal.open({
                templateUrl: 'careersTpl.html',
                controller : modalContentCtrl,
                resolve: {
                    items: function() {
                        return $scope.data;
                    }
                }
            })
        }
    });
});

var modalContentCtrl = function ($scope, $modalInstance, data) {
    $scope.data = data;
    $scope.selected = {
        item: $scope.data.specs
  };
};

JSON:

{
   "specs":[
      {
         "job-title":"TITLE",
         "job-apply":"applink",
         "job-body":"JOB BODY"
      }
   ]
}

HTML:

<div class="car-up">
     <script type="text/ng-template" id="careersTpl.html">
        <div class="modal-header">
            <h3>Lorem Ipsum</h3>
        </div>   
        <div class="modal-body">
            <p ng-repeat="item in data">{{item}}</p>
        </div>
     </script>    
     <button class="btn" ng-click="open()">Open</button>
</div>

I'm new to AngularJS, but I have linked the app.js and ctrl.js... thanks.

EDIT: after I've placed ng-controller="carCtrl" in the html file, I receive this error:

Error: [$injector:unpr] http://errors.angularjs.org/1.5.7/$injector/unpr?p0=%24modalInstanceProvider%20%3C-%20%24modalInstance O/<@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:6:412 db/n.$injector<@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:43:84 d@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:40:344 db/V<@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:43:144 d@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:40:344 e@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:41:78 h/<.invoke@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:41:163 gf/this.$gethttps://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:89:397 resolveSuccess@https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.2.0/ui-bootstrap-tpls.js:4422:34 e/<@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:130:409 vf/this.$gethttps://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:145:103 vf/this.$gethttps://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:142:165 vf/this.$gethttps://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:145:399 Lc[b]https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:274:444 Sf@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:37:31 Rf/d@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:36:486

2
do a console.log in your open function to see if it is firinguser2085143
why you had defined $scope.open inside $http.get call success function.. does that ajax call get succeed?Pankaj Parkar
did you informed the controller in the html ? like ng-controller="controllerName"Leonardo Lobato
Nope. nothing. Even the data is not getting logged.eric.dummy
I have edited the question. Please check.eric.dummy

2 Answers

1
votes

Please find working demo

angular.module('carApp', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
var app = angular.module('carApp');
app.controller('carCtrl', function($scope, $http, $uibModal) {
  //$http.get('jobs.json').success(function(data) {//Uncomment
  //$scope.data = data; Uncomment 

  //Remove below line from code when you are using this in your project
  $scope.data = {
    "specs": [{
      "job-title": "TITLE",
      "job-apply": "applink",
      "job-body": "JOB BODY"
    }]
  }

  $scope.open = function() {

      var modalContent = $uibModal.open({
        templateUrl: 'careersTpl.html',
        controller: 'ModalInstanceCtrl',
        controllerAs: '$ctrl',
        resolve: {
          items: function() {
            return $scope.data;
          }
        }
      })
    }
    //});//Uncomment
});

app.controller('ModalInstanceCtrl', function($uibModalInstance, items, $scope) {
  $scope.data = items;
  console.log($scope.data);
  $scope.selected = {
    item: $scope.data.specs
  };

});
<!doctype html>
<html ng-app="carApp">

<head>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-animate.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-sanitize.js"></script>
  <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.3.0.js"></script>

  <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>

  <div ng-controller="carCtrl" class="modal-demo">
    <script type="text/ng-template" id="careersTpl.html">
      <div class="modal-header">
        <h3>Lorem Ipsum</h3>
      </div>
      <div class="modal-body">
        <p ng-repeat="(k,v) in data.specs">
          <span>Title: {{v["job-title"]}}<br/> </span>
          <span>Link: {{v["job-apply"]}}<br/> </span>
          <span>Body: {{v["job-body"]}}<br/> </span>
        </p>
      </div>
    </script>
    <button class="btn" ng-click="open()">Open</button>
  </div>
</body>

</html>
1
votes

Try defining the controller like this outside,

app.controller('modalContentCtrl ', function($scope, $modalInstance, data) {
  $scope.data = data;
  $scope.selected = {
    item: $scope.data.specs
  };

}