角JS:NG单击范围集不起作用NG-如果 [英] Angular JS: ng-click scope set doesn't work in ng-if

查看:115
本文介绍了角JS:NG单击范围集不起作用NG-如果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我在angularjs看到了一个错误:

Today, I have seen a bug in angularjs:

当您尝试直接在NG-点击设置一个范围值,它不会当你的NG-点击是在工作中,NG-如果这考验的相同的范围值 - >的http://jsfiddle.net/9j2TL/26/

When you try to set a scope value directly in a ng-click, it doesn't work when your ng-click is in a ng-if which test the same scope value -> http://jsfiddle.net/9j2TL/26/

angular.module('test', [])
.controller('testCtrl', function($scope) {
    $scope.step = 1;

    $scope.setStep = function(step) {
         $scope.step = step;  
    };
});

<div ng-app="test">
<div ng-controller="testCtrl">
    <ul class="timeline">
        <li>
            <div class="block-submit">
                <button class="btn btn-primary btn-lg" ng-click="step = 2">Without ngif block</button>
            </div>
        </li>

        <li ng-if="step > 1">
            <div class="block-submit">
                <button class="btn btn-primary btn-lg" ng-click="step = 3">with ngif block</button>
            </div>
        </li>

        <li ng-if="step > 1">
            <div class="block-submit">
                <button class="btn btn-primary btn-lg" ng-click="setStep(3)">With ngif block and scope function</button>
            </div>
        </li>
    </ul>
    <p>
        step value : {{ step }}
    </p>
</div>
</div>

要解决这个问题,你应该创建一个作用域功能...

To solve it, you should create a scope function...

如果某人有此问题的解释,我会很乐意去了解它!

If somebody have a explication for this problem, I would be happy to understand it !

THX:)

推荐答案

我不认为这是一个错误。你是在视图只是自动生成的属性和混乱的范围。

I don't think this is a bug. You are just auto-creating properties and confusing scopes in the view.

更新提琴手

这不工作:

<li ng-if="step > 1">
    <div class="block-submit">
        <button class="btn btn-primary btn-lg" ng-click="$parent.step = 3">with ngif block</button>
    </div>
</li>

这是发生,因为里面的NG-如果正在创造一个新的范围。

This is happening because self inside of the ng-if is creating a new scope.

这篇关于角JS:NG单击范围集不起作用NG-如果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆