简体   繁体   English

如何连接js和angularjs表达式?

[英]How to CONCATENATE js and angularjs expressions?

I am redirecting to another page using onClick js function. 我正在使用onClick js函数重定向到另一个页面。 But I want to include query string to the given URL. 但是我想将查询字符串包括到给定的URL中。 The query string consists of an ID which I am getting from angularjs expression. 查询字符串包含一个我从angularjs表达式获取的ID。 I am having trouble in combining the two expressions. 我在组合这两个表达式时遇到了麻烦。

Code: 码:

<div class="col-md-1 col-sm-1 padding10 left-border" onclick="window.location=RootURL+'pages/contest/contestdetail.ui.php?cID=83'">{{x.MemberPosition}}</div>

In the above code, I want to replace cID= 83 to something like cID= x.ContestID. 在上面的代码中,我想将cID = 83替换为cID = x.ContestID。 I am getting the value of {{x.ContestID}} correctly. 我正确获取了{{x.ContestID}}的值。

I recommend to do it this way 我建议这样做

Controller: 控制器:

var app = angular.module('App', []);

app.constant('RootUrl', function() {
    return '/';
});

app.controller('YourCtrl', ['$scope', '$location', 'RootUrl' function($scope, $location, RootUrl) {
    $scope.gotoContest = function(id) {
        var url = RootUrl + 'pages/contest/contestdetail.ui.php?cID=' + id;
        $location.url(url);
    };
}]);

View: 视图:

<div class="col-md-1 col-sm-1 padding10 left-border" ng-click="gotoContest(x.ContestID)"></div>

In Angular , you should use $window instead of window Angular中 ,您应该使用$window而不是window

A reference to the browser's window object. 对浏览器窗口对象的引用。 While window is globally available in JavaScript, it causes testability problems, because it is a global variable. 虽然window在JavaScript中全局可用,但由于它是全局变量,因此会引起可测试性问题。 In angular we always refer to it through the $window service, so it may be overridden, removed or mocked for testing 在角度上,我们总是通过$ window服务引用它,因此它可能会被覆盖,删除或模拟以进行测试

Or $location instead of window.location $location代替window.location

The $location service parses the URL in the browser address bar (based on window.location) and makes the URL available to your application. $ location服务解析浏览器地址栏中的URL(基于window.location),并使该URL可用于您的应用程序。 Changes to the URL in the address bar are reflected into the $location service and changes to $location are reflected into the browser address bar. 地址栏中URL的更改会反映到$ location服务中,而$ location所做的更改会反映到浏览器地址栏中。

BTW, if you want to use $location in the view directly, remember to bind $location to the controller's scope like 顺便说一句,如果您想直接在视图中使用$location ,请记住将$location绑定到控制器的作用域,例如

.controller('myController', function($scope, $location) {
  $scope.$location= $location;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM