gpt4 book ai didi

javascript - 如何根据AngularJS中的某些条件进行两个单独的实现

转载 作者:行者123 更新时间:2023-12-02 14:09:52 25 4
gpt4 key购买 nike

我的情况如下

function abc(){
if(someCondition){

// Implemention 1

} else {

// Implemention 2

}

}

This kind of situation is in the whole application, but I want to get my code to be clean. I want to have something like in c#, where by dependency injection I can select between two classes on runtime and switch between classes with Class1 with"Implementaion 1" and Class2 with "Implemention 2". I am using angularjs 1.x

Example : I have approx 20 controllers and services in "Implementation 1" and same 20 controller and services in "Implementation 2". Can you please help me how can can I design my application so that if my application is in "Mode 1" then it should use "Implementation 1" and in runtime if it switched to "Mode 2" than it should use "Implementation 2"

请给一些建议。

最佳答案

你当然可以使用$injector来做到这一点,这是代码:

angular.module('myApp').controller('myCtrl', 
['$injector', '$scope',
function($injector, $scope) {
var service;
if (something) {
service = $injector.get('myService1');
}else{
service = $injector.get('myService2');
}
});

关于javascript - 如何根据AngularJS中的某些条件进行两个单独的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39708548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com