js调用angularjs内部方法,$scope.$apply()强制更新

js调用angularjs内部方法,$scope.$apply()强制更新

1.html

<div id="food" ng-cloak ng-app="food" ng-controller="FoodCtrl">
    <button type="button" onclick="isShow()">点击</button>
    <div class="modal" ng-show="modalStatus">显示或者隐藏</div>
</div>

2.js

function addItem(index) {
    angular.element(document.getElementById('food')).scope().addItem(index);
}

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

myApp.controller('FoodCtrl', ['$scope', function($scope) {
    $scope.modalStatus = false;    
    $scope.isShow= function() {
        this.modalStatus = !this.modalStatus;
        $scope.$apply();
    }
}]);

3.js调用angularjs内部方法,并且改变数据,视图不刷新的情况下,请加上$scope.$apply();

猜你喜欢

转载自blog.csdn.net/weixin_40687883/article/details/86491230