gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:53:50 24 4
gpt4 key购买 nike

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

我正在将代码添加到存在指令的现有代码库中

<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 的建议,以下方法效果很好

在 Controller 中

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

在 HTML 中

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

最佳答案

基于你应该能够使用的指令代码:

ng-msg="myStatus"

这将作为表达式添加到模板中

注意指令中的行:

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

它寻找一个或另一个属性并区别对待它们

关于javascript - 如何在 HTML 中将 $scope 变量的值作为字符串提供?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30090315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com