Deborah Deborah - 6 months ago 43
AngularJS Question

Controller in Nested views of AngularJS

Iam new to AngularJS and was struck at the concept of angular nested views with single controller. Gone through some examples here, which didn't help me. Below is the code from a question and I need 2 things here. After clicking on submit:

1.The date selected has to be assigned as input and url has to be constructed based on the date selected and the result from that url has to be displayed in Modal.

2.At the same time a table(present in tab-submit.html) has to displayed in the page(in tab.html) below the submit button from another URL.

Below is the code I have in app.js:

wc.controller('MyController', function ($scope, $modal, $log, $http, $location, $filter) {
var that = this;

var in10Days = new Date();
in10Days.setDate(in10Days.getDate() + 10);
$scope.dates = {
date3: " "
};
this.dates = {
date3: new Date()
};
this.open = {
date3: false
};
// Disable weekend selection
this.disabled = function (date, mode) {
return (mode === 'day' && (new Date().toDateString() == date.toDateString()));
};
this.dateOptions = {
showWeeks: false,
startingDay: 1
};
this.timeOptions = {
readonlyInput: false,
showMeridian: false
};
this.dateModeOptions = {
minMode: 'year',
maxMode: 'year'
};
this.openCalendar = function (e, date) {
that.open[date] = true;
};

$scope.format = 'yyyy-MM-dd%20HH:mm';
debugger;
$scope.open = function () {
var date = $filter("date")($scope.dates.date3, $scope.format);
$http.get(http://myurlPartA+date+"myurlPartB")
.success(function (response) {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
resolve: {
items: function () {

return response;
}
}

});
});


};
});
var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
$scope.items = items;

$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
};


Here is a
plunker
:http://plnkr.co/edit/xKbkFGoa3p5y5NAzueut?p=preview. Is it possible to get solution for my question??Hope anyone will help me to understand this.

Thanks in advance!!

Requirements

1. Have a page with two tabs

2. If click the tab1, should load the page with date picker and a submit button

3. After selecting a date picker I will click the submit button

4. Then from a url I should get the data for particular date which I have selected.

5. There will be two api calls, one for modal and one for table

6. Then modal should show up with the data

7. After closing the modal, table should below the submit button

Answer

As I understood from your discussion, I think this is what you wanted to do.

  1. Have a page with two tabs
  2. If click the tab1, should load the page with date picker and a submit button
  3. After selecting a date picker I will click the submit button
  4. Then from a url I should get the data for particular date which I have selected.
  5. There will be two api calls, one for modal and one for table
  6. Then modal should show up with the data
  7. After closing the modal, table should below the submit button

I saw few issues in your codes.

  1. Some issues in Directive, the way you use
  2. Getting data from api
  3. How you open and close the modal
  4. How you print data in table

I have a updated, working Plunker here.

Please find the below code changes. In the codes you are getting the codes for Modal. but I dont know how you will bind it. Please change it as you want.

index.html

<!DOCTYPE html>
<html>
<head>
    <style>
        ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
            overflow: hidden;
            background-color: #333;
        }
        li {
            float: left;
        }
        li a {
            display: inline-block;
            color: white;
            text-align: center;
            padding: 14px 16px;
            text-decoration: none;
        }
        li a:hover {
            background-color: darkgrey;
        }
    </style>

    <link rel="stylesheet" type="text/css" href="jquery.datetimepicker.css" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" />

    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="jquery.datetimepicker.full.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
    <script src="ui-bootstrap-tpls.js"></script>
    <script src="datetime-picker.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.min.js"></script>
    <script src="application.js"></script>

</head>
<body ng-app="wc">
    <ul>
        <li><a ui-sref="tab">Tab1</a></li>
        <li><a ui-sref="tabs">Tab2</a></li>
    </ul>
    <div class="container">
        <div ui-view></div>
    </div>
</body>
</html>

application.js

var wc = angular.module('wc', ['ui.router','ui.bootstrap', 'ui.bootstrap.datetimepicker']);

wc.config(function ($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/posts');
    $stateProvider
    .state('tab', {            
        url: '/tab1',
        templateUrl: 'tab.html'
    })        
    .state('tabs', {
        url: '/tabs',
        templateUrl: 'tabs.html',         
    });
});

wc.controller('SampleController', function ($scope, $http, $modal) {

    $scope.subt_click = function () {

        //Selected Date is here use as you want
        //$scope.mydate
        alert($scope.mydate);

        //Modal Data
        $http.get("http://jsonplaceholder.typicode.com/posts")
        .success( function(response) {
            var modalInstance = $modal.open({
                templateUrl: 'myModalContent.html',
                controller: 'ModalController',
                resolve: {
                    items: function () {
                        return response;
                    }
                }
            });
        });

        //Table Data
        $http.get("http://jsonplaceholder.typicode.com/posts")
        .success( function(response) {
            $scope.tableData = response;
        });
    };

});

wc.controller('ModalController', function ($scope, $modalInstance, items) {

    $scope.modalData = items;

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };

});

wc.directive('datetimepicker', function () {
    return {
        require: 'ngModel',
        link: function (scope, el, attr, ngModel) {
            $(el).datetimepicker({
                onSelect: function (dateText) {
                    scope.$apply(function () {
                        ngModel.$setViewValue(dateText);
                    });
                }
            });
        }
    };
});

Tab.html

<div class="jumbotron text-top" ng-controller="SampleController">   
<h4>Select from below:</h4> 
<form class="form-horizontal">
    <input datetimepicker="" ng-model="mydate" type="text" readonly-input="true" />
    <a class="btn btn-info" ng-click="subt_click()">Submit</a>
</form>

<div class="table-responsive" ng-show="tableData.length > 0"> 
    <table class="table table-striped table-bordered table-hover dataTables-example"> 
        <thead> 
            <tr> 
                <th>ID</th> 
                <th>Body</th> 
            </tr> 
        </thead> 
        <tbody> 
            <tr ng-repeat="x in tableData"> 
                <td>{{x.id}}</td> 
                <td>{{x.body}}</td> 
            </tr> 
        </tbody> 
    </table> 
</div> 

<script type="text/ng-template" id="myModalContent.html">
    <div class="modal-header">
        <h3>Info</h3>
    </div>
    <div class="modal-body">
        <ul>
            <li ng-repeat="x in modalData">
                {{ x.id + '-' + x.title}}
            </li>
        </ul>
    </div>
    <div class="modal-footer">
        <button class="btn btn-warning" ng-click="cancel()">Close</button>
    </div>
</script>

Comments