2015-02-17 61 views
0

我嘗試將我的指令放入基於$ scope值的不同模板中。我用NG-重複內部指令,我送她的數據更對象和其他直接輸入AngularJS中的不同templateUrl應用程序

指令:

/** 
* angular-streamlist directive 
*/ 
angular.module('ngStreamlist', []).directive('webcams', function() { 
    return { 
     restrict: 'A', 
     scope: { 
      stream_webcam: '=webcam', 
      stream_margin: '@margin' 
     }, 

     templateUrl: function(elem, attr) { 
      if (scope.stream_webcam.webcam_domain) { 
        if (scope.stream_webcam.webcam_domain == 'youtube') { 
         return 'templates/youtube.html'; 
        } 
        if (scope.stream_webcam.webcam_domain == 'twitcam') { 
         return 'templates/twitcam.html'; 
        } 
      } 
     } 

    }; 
}); 

HTML:

<div data-ng-repeat="webcam in datas.webcams"> 
<div data-webcams data-webcam="webcam" data-margin="no"></div> 
</div> 

和數據是這樣的: { 「id」:1, 「webcam_domain」:「youtube」,...等等 }

我有t他的錯誤:

ReferenceError: scope is not defined 
    at Object.templateUrl (streamlist.js:13) 

我不明白,範圍是IS定義的,不是?

回答

0

問題是scope不是您的templateUrl函數可用的局部變量。它是同一個對象上的一個字段。只要呼叫角碼不會將this設置爲另一個對象,您應該可以通過this.scope訪問它。

0

您需要的範圍添加到您的templateUrl功能以及像這樣:

templateUrl: function(scope, elem, attr) 

否則你的函數將不能訪問它,因此你的錯誤消息。

+0

這就是我說:'無法讀取未定義的屬性'webcam_domain'Object.templateUrl(streamlist.js:13)' – JoDiii 2015-02-17 23:03:38