简体   繁体   English

Angular ng-hide在指令中不起作用

[英]Angular ng-hide is not working in directive

return {
restrict : "AC",
template : + '<div> '
+ '     <span class="smallFont" data-ng-hide="dossierItem.itemDetail.pubdate">{{ item.createdOn | hbdatetime }}</span>'
+ '     <span class="smallFont" data-ng-show="dossierItem.itemDetail.pubdate">{{ item.itemDetail.pubdate | hbdatetimepubdate }}</span>'
+ ' </div>',
link : function(scope, elm, attrs) {
scope.$watch(attrs.itemList, function(value) {
elm.text(value);
});
}

I can see both dates are loading in the inspect element.But it is always showing create date on the view. 我可以看到两个日期都在inspect元素中加载,但是它总是在视图中显示创建日期。 I am showing a detailed info while clicking on this ,if i do that it will display the correct one in both places . 单击此按钮时,我正在显示详细信息,如果这样做,它将在两个地方都显示正确的信息。

But not sure why ng-hide is not working in directive . 但是不确定ng-hide为什么不能在指令中工作。

Please suggest 请建议

Try this one. 试试这个。

Working Demo 工作演示

Problem here you miss open div tag in template portion and make sure assign value before need to create/initialize object. 这里的问题是您错过了模板部分中的打开div标签,并确保在需要创建/初始化对象之前先赋值。

    $scope.dossierItem = {};
    $scope.dossierItem.itemDetail = {};
    $scope.dossierItem.itemDetail.pubdate = new Date();

This cause issue: 这会导致问题:

$scope.dossierItem.itemDetail.pubdate = new Date();

if not initialize dossierItem object it throws errors. 如果未初始化dossierItem对象,则会引发错误。 Please your console error to inspect better. 请检查您的控制台错误以进行更好的检查。 Few things eliminated for clarity. 为清楚起见,几乎没有删除任何东西。

var myApp = angular.module("myApp",[]);

myApp.controller("MainCtrl",function($scope){

    $scope.dossierItem = {};
    $scope.dossierItem.itemDetail = {};
    $scope.dossierItem.itemDetail.pubdate = new Date();

    $scope.validDate = true;

    $scope.item= {},
    $scope.item.createdOn = new Date();

    console.log($scope.dossierItem);
});


myApp.directive("test", function(){

return {
restrict : "AC",
template :'<div>'
    +'<span class="smallFont" ng-hide="dossierItem.itemDetail.pubdate">span1 {{item.createdOn}} </span>'
    +'<span class="smallFont" ng-show="dossierItem.itemDetail.pubdate">span2 {{item.createdOn}}</span>'
+'</div>',
}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM