javascript
  • angularjs
  • angularjs-directive
  • watch
  • 2014-11-14 79 views 1 likes 
    1

    我想要做的東西很簡單:AngularJS指令 - 鏈接功能元素PARAM手錶功能不availble的

    我定義的指令(「A」型)需要處理這個元素時,scope屬性改變。

    JS

    app.directive("autoAnimation", function() 
         { 
          return { 
           restrict: 'A', 
           scope: { 
            animateChanged: '=', 
            maxHeight: '@'//for the first time 
           }, 
           link: function (scope, element, attrs) 
            { 
             var isFirstTime = true; 
             scope.$watch('animateChanged', function (newVal) 
             { 
              console.log(newVal) 
    
                //Trying to get the 'element' but it's not available - what can I do to get the ul element and manipulate it? 
                //why it's not available? 
    
             }); 
    
            } 
          }; 
         }); 
    

    HTML

    <body ng-controller="MainCtrl"> 
        <input type="checkbox" ng-model='isChecked' /> 
        <ul auto-animation animate-changed='isChecked'> 
        <li>Test</li> 
        <li>Test</li> 
        <li>Test</li> 
        <li>Test</li> 
        <li>Test</li> 
        </ul> 
    
        </body> 
    

    當 'animateChanged' 的問題是被改變時, '元素'(和範圍)參數不可用。

    這裏是plunker

    我的問題:

    1)它爲什麼不提供?
    2)當'animateChanged'被改變時,我能做些什麼來操作'ul'元素(聲明指令的地方 - 請參閱HTML)?

    編輯

    我沒有檢查的例子,在這個例子中是可用的(我不好,對不起)。
    但在我的項目中,它不可用,我不能把這個例子帶給我的情況......(在我的項目中,UL使用ng-repeat動態構建)。 任何想法可能會導致這種行爲?

    +0

    看起來對我很好http://plnkr.co/edit/D2MfkqkMEUp5EgibIHDV?p=preview – 2014-11-14 12:59:32

    +0

    你是對的!但它在我的項目中可用,我不知道爲什麼。 – cheziHoyzer 2014-11-14 13:11:01

    回答

    0

    他們是可用的,只是改變你的手錶的內部CONSOLE.LOG(的newval,元素,範圍):

    console.log(newVal, element, scope) 
    

    Plnkr:http://plnkr.co/edit/NZq6G04zsVBg2GgNWZkv?p=preview

    0

    正如你$watch荷蘭國際集團在鏈接功能讓(元素)是可用的,改成這樣:

    console.log(element); 
    

    實際上當checked:true/unchecked:false時,正在輸出點擊元素的狀態。

    +0

    你是對的!但它在我的項目中可用(這只是一個測試)也許在我的項目中的其他事情導致這個問題... – cheziHoyzer 2014-11-14 13:10:26

    +0

    你想要做什麼完全?你想在複選框被選中/取消選中時執行某些操作,如顯示/隱藏? – Jai 2014-11-14 13:14:32

    +0

    是這樣的:http://plnkr.co/edit/C5Ppo8L8wH89gf3pm9j4?p=preview – Jai 2014-11-14 13:16:51

    相關問題