ionic开发——Angularjs页面渲染完成在执行指定操作的方法

在ionic开发中,我们有的方法需要在页面渲染之后在执行,而一般js代码在页面渲染之前就执行了,所以有些东西没有实现。

下面的方法可以实现这个功能:

这里用ng-repeat举例:

html的View里面:

<ul>
  <li ng-repeat="item in items" on-finish-render="callMethod()">
      dummy Text
  </li>
</ul>

我们需要添加一个指令 directive.js:

//angularjs渲染完执行
  .directive('onFinishRender',['$timeout', '$parse', function ($timeout, $parse) {
    return {
        restrict: 'A',
        link: function (scope, element, attr) {
            if (scope.$last === true) {
                $timeout(function () {
                    scope.$emit('ngRepeatFinished'); //事件通知
                        var fun = scope.$eval(attr.onFinishRender);
                        if(fun && typeof(fun)=='function'){  
                            fun();  //回调函数
                        }  
                });
            }
        }
    }
  }])

JS:

//ng-repeat执行完在执行
	$scope.$on('ngRepeatFinished', function(ngRepeatFinishedEvent) {
		$scope.callMethod = function(){
			console.log("渲染完成了")
		}
 	});




猜你喜欢

转载自blog.csdn.net/yu17310133443/article/details/74570038