gpt4 book ai didi

asp.net-mvc - 启用优化时 AngularJS 不起作用 ASP.NET MVC

转载 作者:行者123 更新时间:2023-12-03 20:03:45 24 4
gpt4 key购买 nike

我有一个使用 angularjs 的应用程序,当优化在 BundleConfig.cs 文件中设置为 false 时工作正常。

但是当我将优化设置为 true 时,我的 angular 表达式工作,这意味着 angular 正在加载,但我的服务和 Controller 不工作,其他 jquery/javscript 工作正常。

你知道这里发生了什么吗?
下面是我的一些代码

捆绑配置文件

using System.Configuration;
using System.Web;
using System.Web.Optimization;

namespace HRMS
{
public class BundleConfig
{
// For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/angular")
.Include("~/Scripts/common/angular.js"
));
bundles.Add(new ScriptBundle("~/bundles/app")
.Include("~/Scripts/angular/app.js"
));
bundles.Add(new ScriptBundle("~/bundles/common")
.Include("~/Scripts/common/common.js"));

bundles.Add(new ScriptBundle("~/bundles/services")
.IncludeDirectory("~/Scripts/angular/services", "*.js"));

bundles.Add(new ScriptBundle("~/bundles/controller")
.IncludeDirectory("~/Scripts/angular/controller", "*.js"));


bundles.Add(new ScriptBundle("~/bundles/jquery")
.Include("~/Scripts/common/jquery-{version}.js"
, "~/Scripts/common/jquery-ui.js"));









bundles.Add(new ScriptBundle("~/bundles/metis").Include(
"~/Scripts/common/plugins/metisMenu/metisMenu.min.js",
"~/Scripts/common/sb-admin-2.js"));

// Use the development version of Modernizr to develop with and learn from. Then, when you're
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/common/modernizr-*"));

bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/common/bootstrap.js",
"~/Scripts/common/respond.js"));

bundles.Add(new ScriptBundle("~/bundles/select2").Include(
"~/Scripts/common/plugins/select2/select2.js"));

bundles.Add(new ScriptBundle("~/bundles/tinymce").Include(
"~/Scripts/common/plugins/tinymce/tinymce.min.js"));

bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/sb-admin-2.css",
"~/Content/plugins/metisMenu/metisMenu.min.css",
"~/Content/font-awsm/css/font-awesome.css",
"~/Content/plugins/select2.css",
"~/Content/plugins/select2-bootstrap.css",
"~/Content/jquery-ui.css"));


BundleTable.EnableOptimizations =
bool.Parse(ConfigurationManager.AppSettings["BundleOptimisation"]);

}

}
}

我的布局页面:
<!DOCTYPE html>
<html lang="en" ng-app="allyhrms">
<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>@ViewBag.Title - Ally HRMS</title>

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/common")


@Scripts.Render("~/bundles/angular")
@Scripts.Render("~/bundles/app")
@Scripts.Render("~/bundles/services")
@Scripts.Render("~/bundles/controller")



@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/metis")
@Scripts.Render("~/bundles/tinymce")
@Scripts.Render("~/bundles/select2")
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
@RenderSection("Script", false)
</head>

App.js//用于 Angular 应用
(function () {
var ally = angular.module('allyhrms', []);
})();

我的服务就是用这种风格制作的。例如我的一项服务就像。
位置服务.js
angular.module('allyhrms').service('LocationService', function ($http) {
this.States = function () {
return GetData($http, "/Location/States", {});
};
this.Cities = function (model) {
return PostData($http, "/Location/Cities", { s: model });
}
});

位置 Controller .js
angular.module('allyhrms').controller('LocationController', [
'LocationService', 'ExceptionService', '$scope', function (locationService, exceptionService, $scope) {
$scope.State = {
Id: 0,
StateName: ''
};
$scope.City = {
Id: 0,
StateId: 0,
CityName: ''
}
$scope.States = [];
$scope.Cities = [];
function loadData() {
locationService.States().then(function(response) {
$scope.States = response.data.model;
});

}
loadData();
$scope.onStateChanged = function() {
locationService.Cities($scope.State).then(function(response) {
$scope.Cities = response.data.model;
});
};
}
]);

最佳答案

这是一个很常见的问题,happens when using inline annotation for injecting the dependencies, rather than using an Array of strings .

所以如果你这样做:

.controller('dummyController', function($scope){...})

你应该这样做:
.controller('dummyController', ['$scope', function($scope){...}])

例如:在您的代码中,您正在执行此操作,当代码被缩小时,这将无法正常工作:
angular.module('allyhrms').service('LocationService', function ($http) {
this.States = function () {
return GetData($http, "/Location/States", {});
};
this.Cities = function (model) {
return PostData($http, "/Location/Cities", { s: model });
}
});

你应该这样做:
angular.module('allyhrms').service('LocationService',['$http', function ($http) {
this.States = function () {
return GetData($http, "/Location/States", {});
};
this.Cities = function (model) {
return PostData($http, "/Location/Cities", { s: model });
}
}]);

关于asp.net-mvc - 启用优化时 AngularJS 不起作用 ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26719646/

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