2014-02-20 66 views
0

我已經包括在頁面下面的腳本:如何從外部jQuery腳本中訪問angularJS模型數據?

<script> 
    $(document).ready(function() { 

     $(".fancy-class").chatter({ 
      hostname: 'http://www.domain.com', 
      group: 'moustache', 
      brand: '{{brand.name}}' 
     }); 
    }); 
</script> 

而且我想品牌被設置爲一個品牌,我知道的是存儲在我的角度模式。

我該怎麼做?

+0

我認爲brand.name是錯誤的。只需刪除引號並嘗試 –

回答

0

AngularJS不會像這樣插入腳本。實現需要與角度應用程序交互的非角度代碼的推薦方法是編寫自定義指令。

我創建了一個plunk,說明如何通過指令處理您的示例。

高亮(JS):

angular.module("demo", []) 
    // omitted for brevity 
    .directive('fancyClass', function() { 
     return { 
     restrict: 'C', // directive is triggered via a class name called fancy-class 
     scope: { 
      'brand': '&' // lets us use an attribute named brand to declare the scope value containing the value we want 
     }, 
     link: function(scope, element, attrs) { 
      // initializes the jQuery plugin, using the scope value for brand 
      element.chatter({ 
      hostname: 'http://www.domain.com', 
      group: 'moustache', 
      brand: scope.brand() 
      }); 
     } 
     } 
    }); 

在您的視圖中的代碼使用它:

<span class="fancy-class" brand="fancyValue">Fancy 1</span> 
0

創建自己的全球comunicator這樣既角和jQuery訪問它。