why do you need to compute the index in this way? why not just rely on the order of $registerNgMessage calls?