2015-02-05 9 views
0

나는 새로운 편이며 알림 목록을 작성하려고합니다. 정말 기본. 알림의 요약 목록이 표시됩니다. 그런 다음 사용자가 알림을 클릭하면 알림의 세부 정보가 표시됩니다. 이 JSFiddle은 내가 조립 한 것을 보여줍니다.아약스 요청시 Angular ng-show를 사용하여 세부 정보를 얻으십시오.

이 방법은 오히려 자신의 세부 사항 단지 요약 목록을 표시하는 행동으로 모든 알림을로드하지 않는 게 좋을 작동하지만

var notificaitonsApp = angular.module('notificationsApp', []); 
 
var notificationListCtrl = notificaitonsApp.controller('NotificationListCtrl', [ 
 
     '$scope', '$http', 
 
     function($scope, $http) { 
 

 
      $scope.notifications = getNotifications(); 
 
      
 
      function getNotifications() { 
 
       var Data = [{ 
 
        "id": "1", 
 
        "primary_line": "Missing Timesheet Entry", 
 
        "secondary_line": "Jan 28th", 
 
        "summary_item": false, 
 
        "message": "A notification description. Do something here. Blah, blah, blah",   
 
        "actions": [{ 
 
         "type": "Navigation", 
 
         "url": "http://somedomain.cm/app/234", 
 
         "url_text": "Update" 
 
         },{ 
 
         "type": "Post", 
 
         "url": "http://somedomain.com/app/api/v1/234", 
 
         "url_text": "Approve" 
 
         }] 
 
       },{ 
 
        "id": "2", 
 
        "primary_line": "Purchase Reqest Approval Needed", 
 
        "secondary_line": "Account 333445, Requested by James", 
 
        "summary_item": false, 
 
        "message": "A different notification message. Take action now.", 
 
        "actions": [{ 
 
         "type": "Navigation", 
 
         "url": "http://somedomain.com/pr/requisitions/434", 
 
         "url_text": "Edit" 
 
        },{ 
 
         "type": "Post", 
 
         "url": "http://somedomain.com/pr/api/v1/requisitions/434", 
 
         "url_text": "Approve" 
 
        }] 
 
       }, { 
 
        "id": "3", 
 
        "primary_line": "Multiple Items Need Your Attention", 
 
        "secondary_line": "", 
 
        "summary_item": true, 
 
        "message": "" 
 
       }, { 
 
        "id": "4", 
 
        "primary_line": "Your Time Off Request was Approved", 
 
        "secondary_line": "Jan 28th", 
 
        "summary_item": false, 
 
        "message": "Yet another notification message. You need to do something.", 
 
        "actions": [{ 
 
         "type": "Navigation", 
 
         "url": "http://somedomain.com/pr/requisitions/434", 
 
         "url_text": "Edit" 
 
        }] 
 
       }]; 
 
      
 
       return Data; 
 
      } 
 
      
 
      $scope.showNotificationDetail = function(notificationId) { 
 
       $('div#' + notificationId).toggle(300); 
 
      } 
 
      
 
      $scope.doNotificationPost = function(postUrl) { 
 
       console.log("POST: " + postUrl); 
 
      } 
 
      
 
      $scope.doNotificationNavigation = function(navigationUrl) { 
 
       console.log("Navigation: " + navigationUrl); 
 
      } 
 
     }]);
body { 
 
    background-color: #F8F6ED 
 
} 
 
ul 
 
{ 
 
    margin-top: 0; 
 
    margin-bottom: 2px; 
 
    padding: 0; 
 
} 
 
li 
 
{ 
 
    font-family: Georgia; 
 
    background-color: #EFE7D5; 
 
    list-style: none; 
 
    margin-left: 0px; 
 
} 
 
a 
 
{  
 
    text-decoration: none; 
 
} 
 
#primary 
 
{ 
 
    padding: 1% 2% 0 2%; 
 
    font-size: 18px; 
 
    color: #978778; 
 
} 
 
#secondary 
 
{ 
 
    padding: 0 2% 1% 2%; 
 
    font-size: 14px; 
 
    color: #5B4F48; 
 
} 
 

 
div.notification_hidden { 
 
    display: none; 
 
} 
 

 
.inline { 
 
    display: inline-block; 
 
    vertical-align: middle; 
 
} 
 

 
.submit { 
 
    margin-left: auto; 
 
    margin-right; auto; 
 
    text-align: right;  
 
} 
 

 
button.notification_button { 
 
    width: 85px; 
 
    height: 30px; 
 
} 
 

 
button.action_button { 
 
    background-color: #cf6b28; 
 
    border: 0 none; 
 
    border-radius: 4px; 
 
    color: #fff; 
 
    cursor: pointer; 
 
    font-weight: bold; 
 
    padding: 6px 15px; 
 
} 
 

 
button.modify-button { 
 
    background-color: #978778; 
 
    color: #fff; 
 
    cursor: pointer; 
 
    border: 0 none; 
 
    border-radius: 4px; 
 
    font-weight: bold; 
 
    padding: 6px; 15px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="notificationsApp"> 
 
    <div ng-controller="NotificationListCtrl"> 
 
     <div> 
 
      Notifications 
 
     </div> 
 
     <div id="notifications"> 
 
      <div ng-if="notifications.length == 0"> 
 
       No Notifications 
 
      </div> 
 
      <ul ng-if="notifications.length > 0" ng-repeat="notification in notifications"> 
 
       <a ng-if="notification.summary_item == false" ng-href="#here" ng-click="showNotificationDetail(notification.id)"> 
 
        <li id="primary">{{notification.primary_line}}</li> 
 
        <li id="secondary">{{notification.secondary_line}}</li> 
 
       </a> 
 
       <a ng-if="notification.summary_item == true" href="{{notification.summary_url}}" id="{{notification.id}}"> 
 
        <li id="primary">{{notification.primary_line}}</li> 
 
       </a> 
 
       <div class="notification_hidden" id="{{notification.id}}"> 
 
        <div class="inline-block"> 
 
         <div class="inline" id="primary_line"> 
 
          {{notification.primary_line}} 
 
         </div> 
 
         <div class="inline" style="float: right"> 
 
          <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAAKCAYAAABi8KSDAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwgAADsIBFShKgAAAABh0RVh0U29mdHdhcmUAcGFpbnQubmV0IDQuMC41ZYUyZQAAAH1JREFUKFN1kEsKwCAMRL3/4Vzorq2fiJsW75A6UkVTFB46SYYkqlJuTslzPWpHzzfh/cExXi0gQTyEsz6/YoCANEDPsZEAMPSWuKV5KQYxOrZWV5Orcs0tAhA5NkYz0X/pRaBtHwOm7RhyGSCX3hZ2ZoPKmcY/7sD8pTz8AhpjX7xHPMGHAAAAAElFTkSuQmCC" alt="Close"/> 
 
         </div> 
 
        </div> 
 
        <div id="message_container"> 
 
         {{notification.message}} 
 
        </div> 
 
        <div class="submit"> 
 
         <div class="inline" ng-repeat="action in notification.actions"> 
 
          <div class="inline" ng-if="action.type == 'Navigation'"> 
 
           <button class="notification_button modify-button" ng-click="doNotificationNavigation(action.url)">{{action.url_text}}</button> 
 
          </div> 
 
          <div class="inline" ng-if="action.type == 'Post'"> 
 
           <button class="notification_button action_button" ng-click="doNotificationPost(action.url)" type="submit">{{action.url_text}}</button> 
 
          </div> 
 
         </div> 
 
        </div> 
 
       </div> 
 
      </ul> 
 
     </div> 
 
    </div> 
 
</div>
. 요약 목록을 표시하고 사용자가 알림을 클릭하면 Ajax 요청을 사용하여 알림 세부 정보를 얻은 다음 세부 정보를 표시합니다.

어떻게 접근해야할지 모르겠습니다. 지시문과 템플릿이 필요하지만 통지 세부 템플릿의 표시를 어떻게 토글할지 모르는 모호한 생각이 있습니다. 나는 더 간단한 해결책이 있기를 바라고있다. 누군가 올바른 방향으로 나를 가리킬 수 있습니까?

+0

간단히 AJAX 요청을하고 $ 범위를 초기화하십시오.세부 변수를 성공 콜백에서 가져오고 ng-show 또는 ng-if를 사용하여 세부 정보를 표시하는 div에 표시합니다. 당신이하는 것처럼 컨트롤러에서 DOM 조작을하지 마십시오. –

+0

알았어요. DOM 조작이 없습니다. 나는 당신이 [여기] (http://jsfiddle.net/DexLab/Lf67f7jc/2/)에서 제안한 것을 시도했다. 그러나 한 번에 하나의 알림에 대해서만 세부 사항을 표시 할 수 있습니다. 귀하의 의견 및이 오래된 JSFiddle 통해 나 에게이 작업을 할 수있는 방법에 대한 아이디어를주지 않았다. 그것이 작동하면 나는 업데이 트됩니다. –

+0

나는 해결책을 찾고있다. 지금은 조금 시간을주고 대답을 게시 할 수있다. – Bricktop

답변

0

스코프 조작을 많이 사용하고 일반적으로 범위를 사용하는 문제에 대한 솔루션을 모았습니다 (각도가 너무 넓지는 않은 것으로 보았 기 때문에 각도에 대해 자세히 알아야 할 것으로 생각했습니다. 범위).

우선 내 코드는 this plnkr에서 찾을 수 있습니다. 그리고 여기 :

Index.html을

<!DOCTYPE html> 
<html> 

    <head> 
    <script data-require="[email protected]" data-semver="1.4.0-beta.3" src="https://code.angularjs.org/1.4.0-beta.3/angular.js"></script> 
    <link href="style.css" rel="stylesheet" /> 
    <script src="script.js"></script> 
    </head> 

    <body> 
<div ng-app="notificationsApp"> 
    <div ng-controller="NotificationListCtrl"> 
     <div> 
      Notifications 
     </div> 
     <div id="notifications"> 
      <div ng-if="notifications.length == 0"> 
       No Notifications 
      </div> 
      <ul ng-if="notifications.length > 0" ng-repeat="notification in notifications"> 
       <a ng-if="notification.summary_item == false" ng-href="#here" ng-click="$parent.showdetail = !showdetail;loadData($parent);"> 
        <li id="primary">{{notification.primary_line}}</li> 
        <li id="secondary">{{notification.secondary_line}}</li> 
       </a> 
       <a ng-if="notification.summary_item == true" href="{{notification.summary_url}}" id="{{notification.id}}"> 
        <li id="primary">{{notification.primary_line}}</li> 
       </a> 
       <div id="{{notification.id}}" ng-show="showdetail"> 
        <div class="inline-block"> 
         <div class="inline" id="primary_line"> 
          {{notification.primary_line}} 
         </div> 
         <div class="inline" style="float: right"> 
          <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAAKCAYAAABi8KSDAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwgAADsIBFShKgAAAABh0RVh0U29mdHdhcmUAcGFpbnQubmV0IDQuMC41ZYUyZQAAAH1JREFUKFN1kEsKwCAMRL3/4Vzorq2fiJsW75A6UkVTFB46SYYkqlJuTslzPWpHzzfh/cExXi0gQTyEsz6/YoCANEDPsZEAMPSWuKV5KQYxOrZWV5Orcs0tAhA5NkYz0X/pRaBtHwOm7RhyGSCX3hZ2ZoPKmcY/7sD8pTz8AhpjX7xHPMGHAAAAAElFTkSuQmCC" alt="Close"/> 
         </div> 
        </div> 
        <div id="message_container"> 
         {{notification.ressource.message}} 
        </div> 
        <div class="submit"> 
         <div class="inline" ng-repeat="action in notification.ressource.actions"> 
          <div class="inline" ng-if="action.type == 'Navigation'"> 
           <button class="notification_button modify-button" ng-click="doNotificationNavigation(action.url)">{{action.url_text}}</button> 
          </div> 
          <div class="inline" ng-if="action.type == 'Post'"> 
           <button class="notification_button action_button" ng-click="doNotificationPost(action.url)" type="submit">{{action.url_text}}</button> 
          </div> 
         </div> 
        </div> 
       </div> 
      </ul> 
     </div> 
    </div> 
</div> 
    </body> 

</html> 

그리고 내 스크립트를 :

내가하는 데 사용하는 범위 변수 showdetail을 추가 : 내가 무슨 짓을했는지에 대한 짧은 설명이

var notificaitonsApp = angular.module('notificationsApp', []); 
var notificationListCtrl = notificaitonsApp.controller('NotificationListCtrl', [ 
     '$scope', '$http','$timeout', 
     function($scope, $http, $timeout) { 

      $scope.notifications = getNotifications(); 

      $scope.showdetail = false; 

      function getNotifications() { 
       var Data = [{ 
       "id": "1", 
       "primary_line": "Missing Timesheet Entry", 
       "secondary_line": "Jan 28th", 
       "summary_item": false, 
       "ressource":{ 
        "message": "loading ..." 
       } 
       },{ 
        "id": "2", 
        "primary_line": "Purchase Reqest Approval Needed", 
        "secondary_line": "Account 333445, Requested by James", 
        "summary_item": false, 
        "ressource":{ 
         "message": "loading ..." 
        } 
       }, { 
        "id": "3", 
        "primary_line": "Multiple Items Need Your Attention", 
        "secondary_line": "", 
        "summary_item": true, 
        "message": "" 
       }, { 
        "id": "4", 
        "primary_line": "Your Time Off Request was Approved", 
        "secondary_line": "Jan 28th", 
        "summary_item": false, 
        "ressource":{ 
         "message": "loading ...", 
         "actions":[] 
        } 
       }]; 

       return Data; 
      } 


      $scope.loadData = function(parent) { 
      $timeout(function(){},1000).then(function(result){ 
       var value = { 
        "message": "A notification description. Do something here. Blah, blah, blah",   
        "actions": [{ 
         "type": "Navigation", 
         "url": "http://somedomain.cm/app/234", 
         "url_text": "Update" 
         },{ 
         "type": "Post", 
         "url": "http://somedomain.com/app/api/v1/234", 
         "url_text": "Approve" 
         }] 
       }; 
       parent.notification.ressource = value; 
      }); 
      }; 


      $scope.doNotificationNavigation = function(navigationUrl) { 
       console.log("Navigation: " + navigationUrl); 
      } 
     }]); 

각 알림을 숨기고 표시하십시오. 이것이 작동하는 방식을 이해하려면 모든 ng-if 및 ng-repeat가 새로운 분리 된 범위를 생성한다는 것을 알아야합니다. 즉, $parent.showdetail = !showdetail;은 부모 범위의 showdetail 값을 변경하며 ng-repeat에서 ng-if가 반복되어 사용되므로 ng-repeat의 범위가 실제로 변경됩니다.

ng-show의 범위가이 예에서 ng-repeat의 범위이기 때문에 이제 ng-show가이 범위 변수로 업데이트됩니다. 정적 데이터 만 표시되므로 로딩 메시지 만 나타나야합니다.

ng-click의 loadData($parent);은 컨트롤러에서 함수를 호출하고 부모 범위 객체를 변수로 전달합니다. 우리 컨트롤러에서 우리는 먼저 API 호출을 모방하기 위해 하나의 스코어를 기다린 다음 우리에게 주어진 객체를 변경합니다. 즉, ng-repeat의 범위를 변경하고 API에서 얻은 데이터를 범위에 삽입하는 것을 의미합니다. Angulars 데이터 바인딩은 이제 명시 적으로 변수를 보거나 폴링하지 않고 값이 업데이트되고 표시되도록합니다.

지시어를 만들거나 추가 시계를 사용하지 않고 올바른 데이터가 표시되어야합니다. 이 작업을 잘 수행 할 수 있지만 코드에서보다 명확한 구조를 원한다면 대신 지시문을 사용할 수 있습니다.

나는 이것이 당신에게 도움이되었고 당신의 문제를 해결하기를 희망합니다. 불분명 한 것이 있으면 알려주십시오.

+0

좋은 답변 주셔서 감사합니다. 이것은 내가 필요한 것입니다. –

관련 문제