我总结难以运行$ apply

时间:2018-05-28 12:52:58

标签: angularjs angularjs-scope

当我点击其中一个时,我有元素列表,我填充模板然后我将其复制到新的DIV,我有一个空模板,当我使用$scope.$apply()时,我收到了一个错误。

$scope.tache_list.forEach(element => {
  $scope.var1 = element;  
  $scope.$apply();
  $('#div2').append($("#div1").html());  
});

我得到Error $rootScope:inprog,我能做什么?

Error rootScope

3 个答案:

答案 0 :(得分:1)

请查看以下article关于$ digest和$ apply

的信息

您的import UIKit import ChameleonFramework import CDAlertView class ColorTableViewController: UITableViewController { var chameleonColorNames = ["Maroon (dark)","Maroon (light)", "Red (dark)","Red (light)", "Watermelon (dark)","Watermelon (light)", "Orange (dark)","Orange (light)", "Yellow (dark)","Yellow (light)", "Lime (dark)","Lime (light)", "Green (dark)","Green (light)", "Mint (dark)","Mint (light)", "Forest Green(dark)","Forest Green(light)", "Teal (dark)","Teal (light)", "Navy Blue(dark)","Navy Blue(light)", "Blue (dark)","Blue (light)", "Sky Blue(dark)","Sky Blue(light)", "Powder Blue(dark)","Powder Blue (light)", "Plum (dark)","Plum (light)", "Purple (dark)","Purple (light)", "Magenta (dark)","Magenta (light)", "Pink (dark)","Pink (light)", "Brown (dark)","Brown (light)", "Coffee (dark)","Coffee (light)", "Sand (dark)","Sand (light)", "Black", "Gray (dark)","Gray (light)", "White (dark)","White (light)"] var chameleonColors = [UIColor.flatMaroonDark,UIColor.flatMaroon, UIColor.flatRedDark,UIColor.flatRed, UIColor.flatWatermelonDark,UIColor.flatWatermelon, UIColor.flatOrangeDark,UIColor.flatOrange, UIColor.flatYellowDark,UIColor.flatYellow, UIColor.flatLimeDark, UIColor.flatLime, UIColor.flatGreenDark,UIColor.flatGreen, UIColor.flatMintDark,UIColor.flatMint, UIColor.flatForestGreenDark,UIColor.flatForestGreen, UIColor.flatTealDark,UIColor.flatTeal, UIColor.flatNavyBlueDark,UIColor.flatNavyBlue, UIColor.flatBlueDark,UIColor.flatBlue, UIColor.flatSkyBlueDark,UIColor.flatSkyBlue, UIColor.flatPowderBlueDark,UIColor.flatPowderBlue, UIColor.flatPlumDark,UIColor.flatPlum, UIColor.flatPurpleDark,UIColor.flatPurple, UIColor.flatMagentaDark,UIColor.flatMagenta, UIColor.flatPinkDark,UIColor.flatPink, UIColor.flatBrownDark,UIColor.flatBrown, UIColor.flatCoffeeDark,UIColor.flatCoffee, UIColor.flatSandDark,UIColor.flatSand, UIColor.flatBlack, UIColor.flatGrayDark,UIColor.flatGray, UIColor.flatWhiteDark,UIColor.flatWhite] override func viewDidLoad() { super.viewDidLoad() // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = false // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows return chameleonColorNames.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "colorCell", for: indexPath) as! ColorTableViewCell // Configure the cell... cell.colorNameLabel.text = chameleonColorNames[indexPath.row] cell.colorView.backgroundColor = chameleonColors[indexPath.row] return cell } override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 43.5 } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("\(chameleonColorNames [indexPath.row])") let selectedThemeColor = chameleonColors[indexPath.row] let alert = CDAlertView(title: "Theme", message: " Apply \(chameleonColorNames[indexPath.row]) theme?" , type: .notification) alert.circleFillColor = selectedThemeColor alert.hideAnimations = { (center, transform, alpha) in transform = CGAffineTransform(scaleX: 0.3, y: 0.3) alpha = 0 } let doneAction = CDAlertViewAction(title: "Yes", handler: { action in self.applyTheme(selectedColor: selectedThemeColor) self.navigationController?.popViewController(animated: true) }) let noAction = CDAlertViewAction(title: "No") alert.add(action: doneAction) alert.add(action: noAction) alert.show() } func applyTheme(selectedColor: UIColor) { Chameleon.setGlobalThemeUsingPrimaryColor(selectedColor, with: .contrast) navigationController?.navigationBar.barTintColor = selectedColor let contrastingColor = UIColor(contrastingBlackOrWhiteColorOn:selectedColor, isFlat: true) navigationController?.navigationBar.titleTextAttributes = [.foregroundColor : contrastingColor] saveThemeColors(thmColor: selectedColor, contrastColor: contrastingColor) } func saveThemeColors(thmColor: UIColor,contrastColor: UIColor) { UserDefaults.standard.set(thmColor, forKey: "themeColor") UserDefaults.standard.set(contrastColor, forKey: "contrastThemeColor") } } extension UserDefaults { func set(_ color: UIColor, forKey key: String) { set(NSKeyedArchiver.archivedData(withRootObject: color), forKey: key) } func color(forKey key: String) -> UIColor? { guard let data = data(forKey: key) else { return nil } return NSKeyedUnarchiver.unarchiveObject(with: data) as? UIColor } } 错误是因为您从inprogress内拨打了$apply()。您只想从$apply block开始新转弯的角度代码调用$ apply。因此,如果你的forEach中有outside,你可以在setTimeout中调用$ apply来告诉你希望它更新的角度。

答案 1 :(得分:1)

在$ timeout函数中包装你的$ scope。$ apply call。

$timeout(function(){
   $scope.$apply()
});

原因:摘要周期将移至事件循环,并在现有周期完成时执行。

答案 2 :(得分:0)

我通过

解决了这个问题
$timeout(function(){
  $scope.$apply()
})
.then(function(){
  ...
});

感谢所有人。