如何在HTML中提供$ scope变量的值作为字符串?

时间:2015-05-07 01:17:15

标签: javascript angularjs

是的,这是一个奇怪的问题。

我正在将代码添加到存在指令

的现有代码库中
<my-progress ng-progress="processingReport" msg="someString"></my-progress>

问题是msg需要取消引用字符串。

我将范围变量设为$scope.myStatus,但提供msg={{myStatus}}不会产生任何内容

有没有办法取消引用$scope.myStatus的价值,以便msg只收到其价值?

更新

该指令看起来像

.directive('myProgress', function($compile) {
      return {
        restrict: 'E',
        link: function(scope, element, attrs){
          var msg = attrs.msg?attrs.msg: "{{"+attrs.ngMsg+"}}";
            var template = '<p ng-if="'+ attrs.ngProgress +'"><img src="img/busy-20.gif" alt=""> '+ msg +'</p>';
            var el = $compile(template)(scope);
            element.replaceWith(el);
          }
      };
    })

更新1
根据 @charlietfl 推荐,以下效果很好

在控制器

$scope.runningStatus = {progressStatus: 'Not Started'};

在HTML中

<my-progress ng-progress="processingReport" ng-msg="runningStatus.progressStatus"></my-progress> 

2 个答案:

答案 0 :(得分:1)

该指令有点破解,应该重写,但它应该接受插值表达式:

Application.EnableEvents = False

请勿忘记<my-progress ng-progress="processingReport" msg="{{myStatus}}"></my-progress>

周围的双引号"

如果它仍然不起作用,您是否可以通过在指令1之后添加此测试元素来测试您的示波器是否正常:

{{myStatus}}

答案 1 :(得分:1)

根据指令代码,您应该可以使用:

ng-msg="myStatus"

哪个会作为表达式添加到模板中

注意指令中的行:

var msg = attrs.msg?attrs.msg: "{{"+attrs.ngMsg+"}}";

查找一个或另一个属性并以不同方式对待它们