0

I am trying to make an app that will display the job details in the modal window, depending on the template that is selected. For this I have combined ui.bootstrap and ui.router . But for some reason, I cannot manage to display the objects as I would want to. I know that $http.get is working, as when I do the console.log(specs), the object is displayed.

This is my code:

HTML

<div class="car-up" ng-controller="carCtrl">
     <script type="text/ng-template" id="careersTpl.html">
        <div class="closer">
            <span class="close-me" ng-click="ok()">X</span>
        </div>
        <div class="modal-body">
            <span>{{placeholder}}</span>
        </div>
        <div class="modal-body modtwo">
            <ul>
                <li><a ui-sref="sales">Sales Department</a></li>
            </ul>
            <ul>
                <li><a ui-sref="webd">Web Developer</a></li>
                <li><a ui-sref="crm">Client Relationship Manager</a></li>
                <li></li>

            </ul>
        <div class="show-me" ui-view></div>
        </div>
     </script> 
     <button class="btn" ng-click="open()">Open</button>
</div>

app.js

var app = angular.module('carApp', ['ngAnimate', 'ngSanitize', 'ui.bootstrap', 'ui.router']);
app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('webd', {
            url: "/web-developer",
            templateUrl: "templates/web-developer.html",
        })
        .state('crm', {
            url: "/crm",
            templateUrl: "templates/crm-uk.html"
        })
}]);

ctrl.js

app.controller('carCtrl', function($scope, $http, $uibModal) {
    $http.get('jobs.json').then(function(response) {
        $scope.placeholder = response.data.default;
        $scope.specs = response.data.specs;

        $scope.open = function() {

            var modalContent = $uibModal.open({
                templateUrl: 'careersTpl.html',
                controller : 'modalContentCtrl',
                controllerAs: '$ctrl',
                size: 'lg',
                backdropClass: 'backdropOver',
                openedClass: 'modal-opened',
                resolve: { 
                    items: function() { return $scope.specs; },
                    items2: function() { return $scope.placeholder;}
                }
            })
        console.log($scope.placeholder);
        console.log($scope.specs);
        console.log($scope.specs.crm);
        }
    });
});

app.controller('modalContentCtrl', function($scope, $uibModalInstance, items, items2) {
    $scope.specs = items;
    $scope.placeholder = items2;
    $scope.ok = function() {
        $uibModalInstance.close();
    }
});

crm-uk.html

<div ng-repeat="(k, v) in specs.crm">
    <h3>{{v["job-title"]}}</h3>
    <p>{{v["job-body"]}}</p>
    Apply Here:
    <p>{{v["job-apply"]}}</p>
</div>

web-developer.html

<div ng-repeat="(k, v) in specs.web-dev">
    <h3>{{v["job-title"]}}</h3>
    <p>{{v["job-body"]}}</p>
    Apply Here:
    <p>{{v["job-apply"]}}</p>
</div>

JSON

{
   "default":"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
   "specs":{
      "web-dev":{
         "job-title":"Web Developer",
         "job-body":"Lorem Ipsum Body Text",
         "job-apply":"applink"
      },
      "crm":{
         "job-title":"Client Relationship Manager",
         "job-body":"Lorem Ipsum CRM Text",
         "job-apply":"applylink"
      }
   }
}

I believe something is wrong with my .json file or how I am accessing it, but cannot figure out what.

Can someone please help?

thanks.

8
  • try <h3>{{v.job-title}}</h3> Commented Dec 6, 2016 at 12:13
  • Tried already, this is what I get in output: 0 0 Apply Here: 0 0 0 Apply Here: 0 0 0 Apply Here: 0 Commented Dec 6, 2016 at 12:15
  • And why is "Apply Here" repeated always.. Commented Dec 6, 2016 at 12:21
  • I think you're iterating wrongly over the JSON key-values. Refer to this link to know how to do this correctly. Commented Dec 6, 2016 at 12:22
  • Please post a working solution. Thanks.. :) Commented Dec 6, 2016 at 12:35

1 Answer 1

1

First best to change the JSON structure as following:

{
    "default": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    "specs": {
        "web-dev": [{
            "job-title": "Web Developer",
            "job-body": "Lorem Ipsum Body Text",
            "job-apply": "applink"
        }],
        "crm": [{
            "job-title": "Client Relationship Manager",
            "job-body": "Lorem Ipsum CRM Text",
            "job-apply": "applylink"
        }]
    }
}

Make the "crm" as a list of multiples. Then in the view file, you can loop the "crm" specs list.

<div ng-repeat="item in specs.crm">
    {{item['job-title']}}<br/>
    {{item['job-body']}}<br/>
    {{item['job-apply']}}<br/>
</div>

Or use {{::item['job-title']}} for single data binding to limit digest cycles

Working Plunkr here Please note only updated for 'CRM'

Sign up to request clarification or add additional context in comments.

3 Comments

The :: limits binding to only the first set of digest watcher. Meaning no continous watch is set on the view values used. More details can be found here toddmotto.com/one-way-data-binding-in-angular-1-5. It comes handy for data which does not change from within your application, and increase performance.
Thanks mate, first time to see this.
Just for the record I have managed to do this using (k, v) and then calling the value, ie.: {{v["job-title"]}}, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.