Electrides have been extensively investigated, which revealed many exotic properties. Here, we suggest two feasible two-dimensional (2D) ferromagnetic electrides HfBa 4 X 8 (X = Cl, Br), whose ground state displays a quantum anomalous Hall effect (QAHE) state with an easy x-axis magnetization. Excess electrons in these materials are confined in the cavity channel created by Ba 2+ cations and X − anions and considerably affect both the magnetic and topological properties. By rotating the magnetization counterclockwise starting from the x axis, the system can achieve two QAHE states with the opposite Chern number and a period change of the band gap, and a 2D Weyl half-semimetal state will act as a critical point with the magnetization along the y axis. Our findings provide a material family platform for studying magnetic topological phase transition in electrides, which may exhibit unique applications in spintronic devices.