Angular Template条件语句在特定值时不运行

时间:2021-07-26 21:40:41

I have a code that I set in my controller

我有一个我在控制器中设置的代码

$("#pcommentTotal" + id).text(count);

Then HTML

<p id="pcommentTotal{{ tip.id}}"></p>

However, what I WANT to do is if that variable count = 0 OR the text of the p element tag which I set to count ( so same thing)

但是,我想要做的是,如果变量count = 0或我设置为计数的p元素标签的文本(同样的事情)

THEN I DO NOT WANT THIS CODE TO BE PROCESSED

那我不想要这个代码加工

        {{commentCount(tip.id)}}
        <iframe ng-src="{{commentResult}}" id="commentsFrame" align="middle" width="800" height="130" frameborder="0" scrolling="no">
            <p>Your browser does not support iframes.</p>
        </iframe>

So in summary if in my controller the count variable = 0 then DO NOT call the commentCount(...) function , not do I want the iframe to be displayed as well.

所以总结如果在我的控制器中count变量= 0然后不要调用commentCount(...)函数,我也不想显示iframe。

I see that there is ng-if etc.. but I'm trying to not do a bunch of hacky angular code as I learn and build.

我看到有ng-if等...但是我试图在学习和构建时不做一堆hacky角度代码。

Should I do the following?

我应该做以下事情吗?

<div ng-if=pcommentTotal{{tip.id}} != 0   > 

</div>  

Update:

Seems like in my controller this works

似乎在我的控制器中这是有效的

$scope.commentCountNow = count;
console.log($scope.commentCountNow);

HTML

count:{{commentCountNow}}

That is not working, which is strange.

那是行不通的,这很奇怪。

2 个解决方案

#1


1  

You have a lot of missing of tech. that I would avoid. IFrame is "ok" at times, but you are calling out to a service with jquery ajax and that will cause a slow TTFB (time till first byte)

你有很多技术缺失。我会避免的。 IFrame有时是“ok”,但是你用jq​​uery ajax调用一个服务,这会导致一个缓慢的TTFB(到第一个字节的时间)

Anyways if you are having trouble (for whatever reason) with OP suggestion with ng-if / count / setting a $scope variable then it might have to do with the way in which you are doing things with a Callback function and/or this apparent loop

无论如何,如果你在使用ng-if / count /设置$ scope变量的OP建议时遇到麻烦(无论出于什么原因)那么它可能与你使用回调函数和/或这个明显的事情有关环

So if this works

所以如果这样的话

<p id="pcommentTotal{{ tip.id}}"></p>

Then I would change the IFrame to be

然后我会改变IFrame

<iframe ng-src="{{commentResult}}" id="commentsFrame{{tip.id}}" ......>

So then you have a proper unique id to set in your code in the controller, so your saying that this works

那么你在控制器的代码中设置了一个合适的唯一ID,所以你说这是有效的

$("#pcommentTotal" + id).text(count)

If that works, then I would do this

如果可行,那么我会这样做

if (count == 0) {
   $("#commentsFrame" + id).hide();
}

#2


1  

There's nothing wrong using ng-if in the templates. That is why it was created. The bad thing is putting too much logic inside templates. Checking if count === 0 doesn't look like too much, but you can move to controller if it makes more sense. smth like $scope.iframeVisible or whatever it means in your project.

在模板中使用ng-if没有错。这就是创建它的原因。坏事是在模板中加入太多逻辑。检查count === 0是否看起来不太多,但如果它更有意义,你可以移动到控制器。像$ scope.iframeVisible一样或者在你的项目中它意味着什么。

#1


1  

You have a lot of missing of tech. that I would avoid. IFrame is "ok" at times, but you are calling out to a service with jquery ajax and that will cause a slow TTFB (time till first byte)

你有很多技术缺失。我会避免的。 IFrame有时是“ok”,但是你用jq​​uery ajax调用一个服务,这会导致一个缓慢的TTFB(到第一个字节的时间)

Anyways if you are having trouble (for whatever reason) with OP suggestion with ng-if / count / setting a $scope variable then it might have to do with the way in which you are doing things with a Callback function and/or this apparent loop

无论如何,如果你在使用ng-if / count /设置$ scope变量的OP建议时遇到麻烦(无论出于什么原因)那么它可能与你使用回调函数和/或这个明显的事情有关环

So if this works

所以如果这样的话

<p id="pcommentTotal{{ tip.id}}"></p>

Then I would change the IFrame to be

然后我会改变IFrame

<iframe ng-src="{{commentResult}}" id="commentsFrame{{tip.id}}" ......>

So then you have a proper unique id to set in your code in the controller, so your saying that this works

那么你在控制器的代码中设置了一个合适的唯一ID,所以你说这是有效的

$("#pcommentTotal" + id).text(count)

If that works, then I would do this

如果可行,那么我会这样做

if (count == 0) {
   $("#commentsFrame" + id).hide();
}

#2


1  

There's nothing wrong using ng-if in the templates. That is why it was created. The bad thing is putting too much logic inside templates. Checking if count === 0 doesn't look like too much, but you can move to controller if it makes more sense. smth like $scope.iframeVisible or whatever it means in your project.

在模板中使用ng-if没有错。这就是创建它的原因。坏事是在模板中加入太多逻辑。检查count === 0是否看起来不太多,但如果它更有意义,你可以移动到控制器。像$ scope.iframeVisible一样或者在你的项目中它意味着什么。