2016-07-26 82 views
0

我們有這樣的:如何使用HTML標記屬性角轉換爲功能PARAM

<td ... ng-click="someFunction(param)" ...></td> 

該功能在控制器的一個定義的controllers.js:

$scope.someFunction= function (param) { ... } 

和預計param將被翻譯成字符串。我嘗試不同的方法,如:

<td ... ng-click="someFunction('PARAM_TRANSLATION_KEY' | translate)" ...></td> 

或:

<td ... ng-click="someFunction({{'PARAM_TRANSLATION_KEY' | translate}})" ...></td> 

而且別人,但似乎沒有任何合作,並在角轉換文檔和/或我我找不到這個-淨。

任何想法?

回答

0

角翻譯有翻譯服務,您可以將您的js代碼中使用,

https://angular-translate.github.io/docs/#/api/pascalprecht.translate。$翻譯

簡單的傳遞函數爲

ng-click="someFunction('PARAM_TRANSLATION_KEY') 

,並使用函數內的服務像這樣:

function(param) { $translate(param).then(translation) { ... } } 

UPDATE:如果你不能改變的代碼,那麼你可以通過像這樣

  1. 帕拉姆服務添加到範圍,使您可以訪問它
  2. 使用該服務來翻譯和回調
  3. 後調用函數

實施例:

var translations = { 
 
    HEADLINE: 'What an awesome module!', 
 
    PARAGRAPH: 'Srsly!', 
 
    NAMESPACE: { 
 
    PARAGRAPH: 'And it comes with awesome features!' 
 
    } 
 
}; 
 

 
var app = angular.module('myApp', ['pascalprecht.translate']); 
 

 
app.config(['$translateProvider', 
 
    function($translateProvider) { 
 
    // add translation table 
 
    $translateProvider 
 
     .translations('en', translations) 
 
     .preferredLanguage('en'); 
 
    } 
 
]); 
 

 

 
app.controller('myController', function($scope, $translate) { 
 
    $scope.translationServiceCallback = function(param, cb) { 
 
    $translate(param).then(function(translation) { 
 
     cb(translation); 
 
    }); 
 
    } 
 

 
    $scope.log = function(param) { 
 
    console.log(param) 
 
    } 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-translate/2.11.1/angular-translate.min.js"></script> 
 

 

 
<div ng-app="myApp"> 
 
    <div ng-controller="myController"> 
 
    <div> 
 
     <button ng-click="translationServiceCallback('HEADLINE',log)">Translate</button> 
 
     <button ng-click="log('HEADLINE')">WYSIWYG</button> 
 
    </div> 
 
    </div> 
 
</div>

+0

我不能更改函數代碼,即使可以,代碼中還有其他地方用已經轉換的參數調用函數,所以這不起作用 - 參數值需要翻譯傳遞給函數... – Jimo

+0

然後你可以傳遞服務,並使用它傳遞param(Look update) – WalksAway

+0

,因爲翻譯服務返回一個promise,而不是你需要在回調函數中使用該函數的值 – WalksAway