AngularJS Expression in Expression

前端 未结 3 794
长情又很酷
长情又很酷 2021-01-02 17:37

Is there a way to have AngularJS evaluate an expression within model data?

HTML:

{{Txt}}


Model:

3条回答
  •  庸人自扰
    2021-01-02 18:10

    You can use the $interpolate service to interpolate the string...

    function ctrl ($scope, $interpolate) {
        $scope.Txt = "This is some text {{Rest}}";
        $scope.Rest = "and this is the rest of it.";
        $scope.interpolate = function (value) {
            return $interpolate(value)($scope);
        };
    }
    

    {{ Txt }}

    {{ interpolate(Txt) }}

    JSFiddle

提交回复
热议问题