将变量值从控制器传递到AngularJS中的视图

时间:2017-07-04 07:44:47

标签: angularjs

我正在开发一个应用程序,我弹出一个窗口,当它弹出时,下次我将尝试打开它不应该打开。下次应该打开当我关闭弹出窗口时,它应该打开。

现在我正在使用count变量,只要它为1,弹出窗口就会打开,只要它大于或等于2,它就会显示警告。但是现在当我关闭弹出窗口时,它不会将count的值重置为0。 在JSfiddle我尝试使用var self = this;它工作正常,但当我在我的代码上尝试它时,它表示Uncaught Exception Typeerror无法找到在行self.count = 0中定义的属性'count'; 怎么做到这一点?或任何替代解决方案?

<a ui-sref-active="active" ng-click="count=count+1; connectMachine(machine, count)" ng-init="count=0" ><span><i class="fa fa-desktop fa-5x"></i></span></a>

$scope.connectMachine = function(machine, count) {
    var promise = restAPIService.connectMachineService(
            $scope.thisStudentThisBatch.guacProfileId,
            machine.connectionId, $stateParams.batchID).get();
    promise.$promise.then(function(response) {
        var json = JSON.parse(response.data);
        console.log(json.id);
        var dnsUrl = $location.absUrl().split('/');
        dnsUrl = dnsUrl[0] + '//' + dnsUrl[2];
        var apiUrl = dnsUrl + $rootScope.apiUrl + "guacamole/disconnect/"
                + json.id;
        var conn_params = $http.defaults.headers.common.Authorization
                + "++" + apiUrl;
        $scope.machineURL = response.headers.url + "&conn_params="
                + conn_params;
        var params = "height=" + screen.availHeight + ",width="
                + screen.availWidth;

        var NewWin;
        var self = this;
        if ($scope.count == 1) {
            NewWin = window.open($scope.machineURL);
        } else if ($scope.count >= 2) {
            alert("Back Off Back Off");
        }
        function checkWindow() {
            if (NewWin && NewWin.closed) {
                window.clearInterval(intervalID);
                self.count = 0;
            }
        }
        var intervalID = window.setInterval(checkWindow, 500);
    }, function(error) {
        dialogs.error("Error", error.data.error, {
            'size' : 'sm'
        });
    });
}

1 个答案:

答案 0 :(得分:0)

您可以使用$scope上定义的变量来触发按钮的值,而不是使用ng-init

<强> HTML

<div ng-app="myApp">
    <ul ng-controller="TodoCtrl">
        <li class="list-group-item" ng-repeat="todo in todos">{{todo.text}}
            <button class="btn btn-default" ng-click="addLike($index)">value- {{count[$index]}}</button>
        </li>
    </ul>
</div>

<强> JS

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

function TodoCtrl($scope) {
    $scope.todos = [{
        text: 'todo one'
    }, {
        text: 'todo two',
        done: false
    }];
    $scope.count = [0, 0];
    $scope.addLike = function(index) {
        var NewWin;
        $scope.count[index] ++;
        if ($scope.count[index] == 1) {
            NewWin = window.open('https://www.google.com');
        } else if ($scope.count >= 2) {
            alert("Back Off Back Off");
        }

        function checkWindow() {
            if (NewWin && NewWin.closed) {
                window.clearInterval(intervalID);
                $scope.count[index] = 0;
                $scope.$apply();
            }
        }
        var intervalID = window.setInterval(checkWindow, 500);
    };
};

JS Fiddle https://jsfiddle.net/p41dLjmn/