猿问
下载APP

请问该如何解决ie9下 对angularjs scope对象 不支持"$apply?

如何解决ie9下 对angularjs scope对象 不支持"$apply


狐的传说
浏览 97回答 4
4回答

富国沪深

AngularJS 外部的控制器(DOM 事件、外部的回调函数如 jQuery UI 空间等)调用了 AngularJS 函数之后,必须调用$apply。在这种情况下,你需要命令 AngularJS 刷新自已(模型、视图等),$apply 就是用来做这件事情的。

芜湖不芜

解决方案:var el=$(this);$scope.$apply(function () {$scope.currentChallenge.comments.push({"user": $scope.currentUser,"message": el.val()});});

开满天机

解决方案: var el=$(this); $scope.$apply(function () { $scope.currentChallenge.comments.push({ "user": $scope.currentUser, "message": el.val() }); });

UYOU

AngularJS功能虽然非常强大,但UI上提供的插件不像JQuery那么多,而且只能通过directive定义扩展的UI插件,虽然国外已经提供了一些基于directive的Tree功能实现,但毕竟不像ZTree那样强大,而且Tree是做项目中很长用的一个基本功能。因此,花了一点时间做了一个例子将ZTree应用到AngularJS中。zTree和后台数据的交互首先,肯定是在页面中引入Angularjs的相关脚本,例如模块(e.g. app.js)、控制器(e.g. controller.js)、Angularjs的脚本并进行相关标记的使用,然后引入zTree的样式包和zTreed 脚本,可以参看一下代码:<!DOCTYPE html><html ng-app="app"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><title>树型菜单</title><link href="plugins/bootstrap-3.3.5/css/bootstrap.min.css" rel="stylesheet"><link href="css/zTreeStyle.css" rel="stylesheet"></head><% include ./../include/header.html %><% include ./../include/top-menu.html %><div id="content" class="content clearfix" ng-controller="wtConfigInfo"><ul tree id="tree" style="font:normal 12px/35px 'Arial';color:#dcdcdc;" class="ztree" ng-model="selectNode" value="1" ></div><% include ./../include/footer.html %><script src="plugins/jquery-1.11.3.min.js" type="text/javascript"></script><script src="plugins/bootstrap-3.3.5/js/bootstrap.min.js" type="text/javascript"></script><script src="..//js/angular.min.js" type="text/javascript"></script><script src="..//js/angular/app.js" type="text/javascript"></script><script src="..//js/angular/controllers.js" type="text/javascript"></script><script src="../plugins/zTree/jquery.ztree.all-3.5.js" type="text/javascript"></script></body></html>在上面的在ul标签中添加了指令tree,这样在加载angularjs中,就可通过指令 tree来进行菜单数据的获取。具体的代码可参考以下代码:var app = angular.module('app', []);//树形结构app.directive('tree',function(){return{require:'?ngModel',restrict:'A',link:function($scope,element,attrs,ngModel){var setting = {data :{simpleData:{enable:true}},callback:{beforeClick:function(treeId, treeNode) {//点击菜单时进行的处理var zTree = $.fn.zTree.getZTreeObj("tree");if (treeNode.isParent) {zTree.expandNode(treeNode);return false;} else {window.location.href=treeNode.url;return true;}}}};//向控制器发送消息,进行菜单数据的获取$scope.$emit("menu",attrs["value"]);//此处attrs["value"]为ul中的value值,此处作为标记使用//接受控制器返回的菜单的消息$scope.$on("menuData",function(event,data){$.fn.zTree.init(element, setting, data);//进行初始化树形菜单var zTree = $.fn.zTree.getZTreeObj("tree");var selectName = $("#selectName").val();if(typeof selectName == "undefined" || selectName == ""){zTree.selectNode(zTree.getNodeByParam("id","1"));//默认第一个选中$("#selectName").val(zTree.getSelectedNodes()[0].code);//赋值}else{for(var i =0; i<data.length;i++){if(data[i]["code"] == selectName ){zTree.selectNode(zTree.getNodeByParam("code", data[i]["code"]));}}}});}}});在上述代码中,使用$scope.$emit("menu",attrs["value"]);向父控制器发送请求数据,在控制器代码中可以接受此消息,并使用$http向后台进行数据的请求,并将从数据库中请求来的数据发送个子控制器。控制器的代码可参考如下:function wtConfigInfo($scope,$http){//接受子控制器发送的消息$scope.$on("menu",function(event,params){$http.get("/commonfuncode").success(function(data){//发送消息给子控制器$scope.$broadcast("menuData",dealMenuData(data,params));});});}这样,就完成了zTree和后台数据的交互。利用指令集成ZTree的实例<!doctype html><html ng-app="app"><head><meta charset="utf-8"><title>ZTree</title><link rel="stylesheet" href="css/app.css"><link rel="stylesheet" href="css/bootstrap.css"><link rel="stylesheet" href="css/animations.css"><link rel="stylesheet" href="css/zTreeStyle/zTreeStyle.css"><script src="lib/jquery-1.10.2.min.js"></script><script src="lib/jquery.ztree.all-3.5.js"></script><script src="lib/angular.min.js"></script><script src="app.js"></script></head><body><body ng-controller='MyController'><ul tree class="ztree" ng-model="selectNode"></ul></body><pre>{{selectNode | json}}</pre></body></html>app.js'use strict';/* App Module */var appModule = angular.module('app', []);appModule.directive('tree', function () {return {require: '?ngModel',restrict: 'A',link: function ($scope, element, attrs, ngModel) {//var opts = angular.extend({}, $scope.$eval(attrs.nlUploadify));var setting = {data: {key: {title: "t"},simpleData: {enable: true}},callback: {onClick: function (event, treeId, treeNode, clickFlag) {$scope.$apply(function () {ngModel.$setViewValue(treeNode);});}}};var zNodes = [{ id: 1, pId: 0, name: "普通的父节点", t: "我很普通,随便点我吧", open: true },{ id: 11, pId: 1, name: "叶子节点&nbsp;- 1", t: "我很普通,随便点我吧" },{ id: 12, pId: 1, name: "叶子节点&nbsp;- 2", t: "我很普通,随便点我吧" },{ id: 13, pId: 1, name: "叶子节点&nbsp;- 3", t: "我很普通,随便点我吧" },{ id: 2, pId: 0, name: "NB的父节点", t: "点我可以,但是不能点我的子节点,有本事点一个你试试看?", open: true },{ id: 21, pId: 2, name: "叶子节点2 - 1", t: "你哪个单位的?敢随便点我?小心点儿..", click: false },{ id: 22, pId: 2, name: "叶子节点2 - 2", t: "我有老爸罩着呢,点击我的小心点儿..", click: false },{ id: 23, pId: 2, name: "叶子节点2 - 3", t: "好歹我也是个领导,别普通群众就来点击我..", click: false },{ id: 3, pId: 0, name: "郁闷的父节点", t: "别点我,我好害怕...我的子节点随便点吧...", open: true, click: false },{ id: 31, pId: 3, name: "叶子节点3 - 1", t: "唉,随便点我吧" },{ id: 32, pId: 3, name: "叶子节点3 - 2", t: "唉,随便点我吧" },{ id: 33, pId: 3, name: "叶子节点3 - 3", t: "唉,随便点我吧" }];$.fn.zTree.init(element, setting, zNodes);}};});appModule.controller('MyController', function ($scope) {});实现功能:定义tree这个属性,使<ul tree class="ztree" ng-model="selectNode"></ul>自动变成一个有数据的tree,点击树节点,自动变更model 的selectNode。&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答