简体   繁体   English

消化周期何时触发(如果有的话)?

[英]When will the digest cycle be triggered, if at all?

I have a small problem, I am using a route like 我有一个小问题,我正在使用类似的路线

.when('/beta/profiles/new', {
  controller: 'ProfilesController',
  controllerAs: 'vm',
  templateUrl: 'profiles/new.html',
  resolve: {
    action: function() { return "new"; }
  }
})
.when('/beta/profiles/index', {
  controller: 'ProfilesController',
  controllerAs: 'vm',
  templateUrl: 'profiles/index.html',
  resolve: {
    action: function() { return "index"; }
  }
})

now in my controller I have something like this: 现在在我的控制器中,我有这样的事情:

function ProfilesController(profileService, action) {
  var vm = this,
      permittedActions = ["index", "new"];
  var actions = {
    index: function() {
      vm.hideProfile = {
        currentProfile: null,
        showModal: false,
        setCurrentProfile: function(profile_id) { this.currentProfile = profile_id },
        toggleHide: function() {/*...*/}
      }
      profileService.all().then(function(data) {
        vm.profiles = data.result;
      })
    },
    new: {/*.. */}
  }
  if(permittedActions[action] > -1) actions[action]();
}

now my question is if I hit a link (in the header,lets say) multiple times in succession, each time I hit profiles/index.html should it re-initialize everything ? 现在我的问题是,如果我连续多次点击链接(在标题中,让我们说),那么每次点击profiles/index.html ,它是否应该重新初始化所有内容?

if I want to takethe advantage of angular's dirty checking thing should I put the vm.hideProfile out of the index function ? 如果我想利用angular的脏检查功能,应该将vm.hideProfile放在index函数之外吗? and If I do that should I also not do the same with vm.profiles = [];? 如果执行该操作,是否也应该对vm.profiles = [];进行相同操作?

how/what can I do to check if variables are getting re-initialized and angular's dirty checking is not in play, or is it just common sense!! 我怎么/怎么做才能检查变量是否被重新初始化,以及是否没有进行角度的脏检查,或者这只是常识! or should I just have a separate controller for each action? 还是每个动作我都应该有一个单独的控制器?

and in case of re-initialization, is there any better way so that I can keep the hideProiles inside the index because I really don't need my new and show actions to know about hideProfile , since its unnecessary? 并且在重新初始化的情况下,还有什么更好的方法可以使hideProiles保留在索引中,因为我真的不需要我的new和show操作来了解hideProfile ,因为这是不必要的?

A few things that hopefully can help: 希望可以帮助您解决的一些问题:

  1. Dirty checking is for objects bound to an active $scope (and $rootScope). 脏检查是针对绑定到活动$ scope(和$ rootScope)的对象。 https://docs.angularjs.org/guide/scope https://docs.angularjs.org/guide/scope

  2. To use $scope in your controller, (a) inject it. 要在控制器中使用$ scope,(a)注入它。 eg ProfilesController($scope, profileService, action), (b) put things on it, eg $scope.vm = vm; 例如ProfilesController($ scope,profileService,action),(b)将东西放到上面,例如$ scope.vm = vm;

  3. looking at your code, the raw initialization should be super fast. 查看您的代码,原始初始化应该超级快。

  4. Don't think javascript is secure. 不要认为JavaScript是安全的。 If security is a real concern for your app, protect at the server. 如果安全是您的应用真正关心的问题,请在服务器上进行保护。

Lastly on app arch, since angular is an MVC framework: define functions in your controller, and connect them to actions in the view via $scope. 最后在应用程序架构上,由于angular是MVC框架:在控制器中定义函数,然后通过$ scope将它们连接到视图中的动作。 eg 例如

$scope.index = function() { // do stuff 
  if (action !== "index") {return}; // disallowed by route
  //continue
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM