2014-02-28 16 views
0

我有一個< ul>,它會被服務器填充。但在該控制器中還有一個iframe。當< li>到達時,即使它們在同一個控制器中,它們和iframe之間仍有一些斷開。在注入的元素上獲得ng-click,以更改已經在視圖中的元素的類?

當你點擊其中一個li應該改變iframe上的類,但它不是。但是,如果我在注入iframe的ng-repeat內部移動iframe,它將起作用。

查看

<div class="content" ng-controller="FeedListCtrl"> 
     <ul> 
      <li ng-repeat="item in items"> 
       <div data-link="{{item.link}}" ng-click="articleShowHide='fade-in'"> 
        <div ng-bind-html="item.title" style="font-weight:bold;"></div> 
        <div ng-bind-html="item.description"></div> 
        <!-- it works if i put the iframe here --> 
       </div> 
      </li> 
     </ul> 
     <!-- doesn't work when the iframe is here --> 
     <iframe id="article" ng-class="articleShowHide" src=""></iframe> 
    </div> 

這裏是控制器。它的AJAX調用來獲取數據,每個< LI>

控制器

readerApp.controller('FeedListCtrl', ["$scope", "$http", "FeedListUpdate", function  ($scope, $http, FeedListUpdate) { 
    $scope.setFeed = function (url) { 
     $http.get('feed?id=' + FeedListUpdate.GetCurrentFeedUrl()).success(function (data)  { 
      $scope.items = data.currentFeed.items; 
     }); 
    }; 
}]); 

回答

0

當內部NG-重複你在不同的範圍內,這意味着你沒有設置變量,你認爲你是。使用$家長,這應該工作。其語法是:

<div data-link="{{item.link}}" ng-click="$parent.articleShowHide='fade-in'"> 

其他人發現這一點的注意事項 - 有時添加大括號也有幫助。有關納克級的更多信息,請瀏覽:http://docs.angularjs.org/api/ng/directive/ngClass

一個例子

如果有人想看到這個動作,我放在一起的例子演示了幾個方法來設置類以及展示範圍問題(見:http://plnkr.co/edit/8gyZGzESWyi2aCL4mC9A?p=preview)。這不是很漂亮,但應該很清楚發生了什麼。順便說一下,在這個例子中,方法起作用的原因是作用域不會自動將它們重新定義爲變量的方式,因此它在根作用域中調用方法,而不是在中繼器範圍中設置變量。

祝你好運!

+0

更好的方法是將articleShowHide作爲對象並使用像'articleShowHide.class ='fade-in''這樣的語法,然後綁定到'articleShowHide.class'屬性。 – Chandermani

+0

@Chandermani那也行!一旦OP瞭解範圍的工作方式,就有許多優秀的解決方案! –

相關問題