2016-04-06 50 views
1

的test.html(我的模板):NG綁定未顯示範圍內的變量{{}}

<span data-ng-bind="emailSent.info"></span> 

JS:

$scope.resetPasswordEmail = "[email protected]"; 
// info is taken from a database with different languages 
$scope.emailSent = { 
    info: getInfoFromDatabase() // returns: 'Confirmation email sent to {{resetPasswordEmail}}, check your email.' 
}; 
angular.element(document.body).append($compile($templateCache.get('test.html'))($scope)); 

然而這會導致在頁面上跨度以下:

Confirmation email sent to {{resetPasswordEmail}}, check your email. 

我試圖做「嵌套」範圍變量。我是否必須重新編譯編譯後的模板。 還是有一個適當的angularjs方式來實現這一點。

回答

1

根據您更新的問題,我明白你爲什麼要這樣做。

的關鍵,做到這一點是使用$interpolate

angular.module('app', []). 
 
controller('Ctrl', function($scope, $interpolate, $compile){ 
 
    $scope.resetPasswordEmail = "[email protected]"; 
 
    $scope.emailSent = { 
 
    info: $interpolate(getInfoFromDatabase())($scope) 
 
    }; 
 
    
 
    function getInfoFromDatabase(){ 
 
    return 'Confirmation email sent to {{resetPasswordEmail}}, check your email.' 
 
    } 
 
    angular.element(document.body).append($compile('<span data-ng-bind="emailSent.info"></span>')($scope)); 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="Ctrl"></div>

+0

完美!我知道AngularJS能以某種方式實現這一點。例如+1。 –

+0

謝謝!抱歉,我一開始並不理解你在做什麼。 :) – Shomz