Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我的指令不是';不使用ng bind html指令?_Javascript_Html_Angularjs - Fatal编程技术网

Javascript 为什么我的指令不是';不使用ng bind html指令?

Javascript 为什么我的指令不是';不使用ng bind html指令?,javascript,html,angularjs,Javascript,Html,Angularjs,我得到了以下指令 productApp.directive('notification', function($timeout) { return { restrict : 'E', replace : false, scope : { type: "=", message: "=" }, template: '<div class="alert {{ty

我得到了以下指令

productApp.directive('notification', function($timeout) {
    return {
        restrict : 'E',
        replace : false,
        scope : {
            type: "=",
            message: "="
        },
        template: '<div class="alert {{type}} alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>{{message}}</div>',
        link : function(scope, element, attrs) {
            console.log(attrs);
            $timeout(function() {
                element.hide();
            }, 3000);
        }
    }
});
productApp.directive('notification',function($timeout){
返回{
限制:'E',
替换:false,
范围:{
类型:“=”,
消息:“=”
},
模板:'×;{{message}}',
链接:函数(范围、元素、属性){
控制台日志(attrs);
$timeout(函数(){
元素。隐藏();
}, 3000);
}
}
});
在我看来:

<div ng-bind-html="notificationPoint">
        <!-- <notification type="alert.type" message="alert.msg"></notification>-->
    </div>

在控制器内部,我正在执行以下操作:

$scope.notificationPoint = '<notification type='alert-success' message='this is a test'></notification>'
$scope.notificationPoint=''
因此,正如您可能意识到的,我想制作一个“通知包装器”,它将包含所有通知(child)(使用通知指令),例如,当我在数据库中添加一行、删除一个产品或任何东西时。因此,这个包装器将持续包含出现和消失的通知

在运行源代码时,当我查看源代码时,没有任何内容插入到包装器div中,甚至在使用ng bind html指令时没有传递到指令代码

有人知道这是不是一个bug吗?或者我做错了什么


谢谢

正如其他人在评论中所说,您应该将消息文本传递给指令。但是,请回答您的原始问题:
ng bind html
必须与
$sce
信任的内容一起使用。例如:

app.controller('myCtrl', function($sce) {
  ...
  $scope.notificationPoint = $sce.trustAsHtml('<notification type='alert-success' message='this is a test'></notification>')
  ...
} 
app.controller('myCtrl',函数($sce){
...
$scope.notificationPoint=$sce.trustAsHtml(“”)
...
} 

正如其他人在评论中所说,您应该将消息文本传递给指令。但是,请回答您的原始问题:
ng bind html
必须与
$sce
信任的内容一起使用。例如:

app.controller('myCtrl', function($sce) {
  ...
  $scope.notificationPoint = $sce.trustAsHtml('<notification type='alert-success' message='this is a test'></notification>')
  ...
} 
app.controller('myCtrl',函数($sce){
...
$scope.notificationPoint=$sce.trustAsHtml(“”)
...
} 

我找到了这个通知库,它难以置信地易于安装,易于使用!只需从控制器调用

flash.success = 'Test!';
然后,将服务注入控制器中

您将在包装器中向视图发送一个成功通知(正如我所希望的那样)

该库称为angular flash。这是带有API文档的github


享受和我一样多的乐趣!!!哈哈,太棒了,我喜欢它-

我找到了这个通知库,它难以置信地易于安装,易于使用!只需从控制器调用即可

flash.success = 'Test!';
然后,将服务注入控制器中

您将在包装器中向视图发送一个成功通知(正如我所希望的那样)

该库称为angular flash。这是带有API文档的github


和我一样享受!!!哈哈,太棒了,我喜欢它-

你为什么还要尝试使用
ng bind html
?这不是为了再次传递指令模板!charlie!:“)我还能做些什么呢?”(我希望有一个静态父包装器,其中包含我将使用动态数据从控制器发送到视图的所有通知。将数据文本消息传递到指令,或者可能传递事件,但在指令中配置html如果我像那样在视图中添加……首先,我将无法对其进行任何控制d每当刷新页面时,它就会出现,但不再出现,即使我从控制器调用它,视图中的指令对象本身在关闭后也会被“删除”。这就是为什么我考虑添加一个“可访问”的父包装器您需要更多的功能,比如
ng show
逻辑的标志来更改消息等,并决定什么将控制消息…事件、服务等。您为什么要使用
ng bind html
?这不是为了再次传递指令模板!charlie!:')我还能做些什么来实现这一目标(我希望有一个静态父包装器,其中包含我将使用动态数据从控制器发送到视图的所有通知。将数据文本消息传递到指令,或者可能传递事件,但在指令中配置html如果我像那样在视图中添加……首先,我将无法对其进行任何控制d每当刷新页面时,它就会出现,但不再出现,即使我从控制器调用它,视图中的指令对象本身在关闭后也会被“删除”。这就是为什么我考虑添加一个“可访问”的父包装器您需要更多的功能,如
ng show
逻辑的标志,以更改消息等,并决定什么将控制消息…事件、服务etcI最终使用Angular Flash模块来满足我的需要,请查看!向视图发送通知真是太棒了,所以eaasy和nicely完成了。我最终使用了Angular Flash模块来完成我所需要的,看看吧!向视图发送通知如此简单和出色,真是太神奇了。