Controller in Nested views of AngularJS

Deborah

Iam new to AngularJS and was stuck 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

Nifal Nizar

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Controller in Nested views of AngularJS

From Dev

Angularjs controller for multiple views

From Dev

Angularjs controller for multiple views

From Dev

angularJS nested abstract views

From Dev

Nested views with nested states in AngularJS

From Dev

AngularJS Controller not working in nested Controller

From Dev

ui router - nested views with shared controller

From Dev

AngularJs multiple instances and nested controller

From Dev

Responsibility of a controller in AngularJS - More than two views?

From Dev

Controlling multiple views in one controller in AngularJS

From Dev

angularjs using single controller with two views

From Dev

Controlling multiple views in one controller in AngularJS

From Dev

AngularJS Directive or Controller to seperate views components

From Dev

How to redirect views using the same angularjs controller?

From Dev

angularjs - ui-router multiple nested views

From Dev

ui-router nested views access to multiple controller

From Dev

AngularJS: Initialize nested scope variables in controller

From Dev

How do I initialize a nested controller in AngularJS?

From Dev

Use ng-model in nested Angularjs controller

From Dev

angularjs nested controller does not receive injected service

From Dev

Use ng-model in nested Angularjs controller

From Dev

Automatically instantiate AngularJS controller nested in templateUrl

From Dev

Can I use one controller updating two views in AngularJS?

From Dev

Angularjs: ui-router controller in views throws error

From Dev

AngularJS - Dynamic Default nested views from Json Data not appearing

From Dev

AngularJS UI router: How to configure nested named views?

From Dev

AngularJS - Dynamic Default nested views from Json Data not appearing

From Dev

angularjs - confusion about nested named views with angular-ui-router

From Dev

Getting nested views to work with multiple modules in AngularJS and ui-router

Related Related

  1. 1

    Controller in Nested views of AngularJS

  2. 2

    Angularjs controller for multiple views

  3. 3

    Angularjs controller for multiple views

  4. 4

    angularJS nested abstract views

  5. 5

    Nested views with nested states in AngularJS

  6. 6

    AngularJS Controller not working in nested Controller

  7. 7

    ui router - nested views with shared controller

  8. 8

    AngularJs multiple instances and nested controller

  9. 9

    Responsibility of a controller in AngularJS - More than two views?

  10. 10

    Controlling multiple views in one controller in AngularJS

  11. 11

    angularjs using single controller with two views

  12. 12

    Controlling multiple views in one controller in AngularJS

  13. 13

    AngularJS Directive or Controller to seperate views components

  14. 14

    How to redirect views using the same angularjs controller?

  15. 15

    angularjs - ui-router multiple nested views

  16. 16

    ui-router nested views access to multiple controller

  17. 17

    AngularJS: Initialize nested scope variables in controller

  18. 18

    How do I initialize a nested controller in AngularJS?

  19. 19

    Use ng-model in nested Angularjs controller

  20. 20

    angularjs nested controller does not receive injected service

  21. 21

    Use ng-model in nested Angularjs controller

  22. 22

    Automatically instantiate AngularJS controller nested in templateUrl

  23. 23

    Can I use one controller updating two views in AngularJS?

  24. 24

    Angularjs: ui-router controller in views throws error

  25. 25

    AngularJS - Dynamic Default nested views from Json Data not appearing

  26. 26

    AngularJS UI router: How to configure nested named views?

  27. 27

    AngularJS - Dynamic Default nested views from Json Data not appearing

  28. 28

    angularjs - confusion about nested named views with angular-ui-router

  29. 29

    Getting nested views to work with multiple modules in AngularJS and ui-router

HotTag

Archive