所以我有一个活的网站在这里选项框:http://www.trock.net/#/downloads试图自动选择基于用户操作系统
我所试图做的是预选基于用户操作系统的“选择操作系统”选项。
apps.js位于: http://www.trock.net/js/app.js。
的下载页面就设在这里的原始HTML:www.trock.net/includes/downloads.html
现在,问题是,detectOS功能我似乎并没有工作。我试图将它附加到应用程序上的ng-init
和选择框后的div上ng-init
,但这不起作用。如果我附上ng-click
并点击它,它会按预期工作。
每个选项都有一个ID,这是我用来找到元素,所以我可以将“selected”设置为true。有没有更好的方式使用角度做到这一点?
我该如何去做这项工作,是我过早或太晚调用函数的问题?
的兴趣守则apps.js:
//Auto select Operating System based on detection
$scope.detectOS = function() {
var processorArchitecture = "";
var userOS = "";
if (navigator.userAgent.indexOf("WOW64") != -1 ||
navigator.userAgent.indexOf("Win64") != -1){
processorArchitecture = "64";
} else { //Assume 32 bit
processorArchitecture = "32";
}
//Detect the OS
if (navigator.platform.indexOf("Win") != -1){
userOS = "win";
if (processorArchitecture == "64")
processorArchitecture = "32";
}
else if (navigator.platform.indexOf("Mac") != -1){
userOS = "mac";
if (processorArchitecture == "64")
processorArchitecture = "32";
}
else if (navigator.platform.indexOf("Lin") != -1){
userOS = "lin";
}
//Check for valid detection
if (userOS != "" && processorArchitecture != "") {
//Valid match found
var optionSelectionID = userOS + processorArchitecture;
//Auto detect OS
//We will find only 1 instance of said query
angular.element(document.querySelector("#win32"))[0].selected = true;
}
};
我还做了哪些实际工作,所以必须是别的东西我做的网站(也许在其他的.html页面Ajax风格的装载)一plunker 。
链接:http://plnkr.co/edit/zlmk24aVHeBNz79PjypP?p=preview