Angular ng-href javascript函数

时间:2016-02-15 23:13:46

标签: javascript jquery html angularjs

我需要将href设置为Javascript函数。当我点击它时,没有任何反应,但当我将鼠标悬停在链接上时,它会显示:

unsafe:javascript:ShowManagementdDiv('65','a60f2a16-267e-418d-bb14-d88de3a33b5f','0');

表数据是在我的角度控制器中动态构建的:

contractorService.getemrtabulation()
                .success(function (data) {
                    $scope.emrcolumns = data.EMRTabulationColumns;
                    repeatRow = '<td align="center" valign="middle" style="background-color:Transparent;border-color:Black;border-width:1px;border-style:Solid;padding:5px;white-space:nowrap;"><a class="IncidentColumn" ng-href={{e.hyper}}>Click Here to Review EMR Document</a></td>';
                    firstRow = '<td>EMR Document</td>';
                    for (i = 0; i < $scope.emrcolumns.length; i++) {
                        repeatRow = repeatRow + '<td>{{e.' + $scope.emrcolumns[i].vchAssociatedDetailColumn + '}}</td>';
                        firstRow = firstRow + '<td>' + $scope.emrcolumns[i].vchColumnHeaderText + '</td>'
                    }
                    firstRow = '<tr>' + firstRow + '</tr>';
                    $scope.emrdetail = data.EMRTabulationDetail;
                    angular.forEach($scope.emrdetail, function (value, key) {
                        value.dteExpirationDate = convertDate(value.dteExpirationDate);
                        value.dteDateCompleted = convertDate(value.dteDateCompleted);
                        value.dteEffectiveDate = convertDate(value.dteEffectiveDate);
                    });
                    angular.forEach($scope.emrdetail, function (value, key) {
                        contractorService.getimage(value.EMRDetailID, value.dteEffectiveDate)
                    .success(function (data) {
                        $scope.emrdetail[key].hyper = data;
                    });
                });
                    $scope.emrTable = '<table>' + firstRow + '<tr style="text-align:center" ng-repeat="e in emrdetail">' + repeatRow + '</tr></table>';
                    firstRow = '';
                    repeatRow = '';
                });

我用它在html中调用它:

<div class="row row-relative">
        <div class="col-md-12">
            <div>{{emrQuestion.EMRTabulationID}}{{emrQuestion.vchTabulationSequenceLetter}}.&nbsp;{{emrQuestion.vchClassPropertyName}}</div><br />
            <div dynamic="emrTable"></div><br /><br />
        </div>
    </div> 

该功能位于页面上的<script>标记中:

function ShowManagementdDiv(imageTypeID, Guid, selectedYear) {
    var TargetWidth = 950;
    var TargetHeight = 670;
    bModalPopupActivated = true; window.clearTimeout(t);
    DisplayModalDivExitWithClickSave('box', TargetWidth, TargetHeight, 'http://localhost/PECIMS/DocumentManagement.aspx?eid=' + imageTypeID + '&Edx=' + Guid + '&y=' + selectedYear, 'Close', 'Click to close window.&nbsp;&nbsp;&nbsp;');
}

以下是创建链接的C#代码:

public async Task<ActionResult> GetImage(int emrDetailID, string docDate)
        {
            var columns = await CommonClient.GetEMRTabulationColumnsForClusterID(876);
            var getcolumn = columns.FirstOrDefault(c => c.EMRTabulationColumnID == 1);
            int? imageTypeId = getcolumn.EdataFileImageTypeID;
            UserInfo.intDocumentManagementMode = 13;
            UserInfo.intPerspectiveCompanyID = UserInfo.intMajorID;
            UserInfo.intPerspectiveCompanyTypeID = UserInfo.intMajorCompanyType;
            UserInfo.SegmentID = emrDetailID;
            UserInfo.dteDocumentDate = DateTime.Parse(docDate);
            var token = await CompanyClient.SaveRecallData(UserInfo);
            string strPathAndQuery = Request.Url.PathAndQuery;
            string strUrl = Request.Url.AbsoluteUri.Replace(strPathAndQuery, "/");
            string LinkToImagesApp = "";
            LinkToImagesApp = AppendProtocolAndHostHeadersPathToWebConfigPath("LinkToImagesApplication");
            string javaLink = strUrl + LinkToImagesApp + "/DocumentManagement.aspx?eid=";
            string docLink;
             string address = "javascript:ShowManagementdDiv('" + imageTypeId + "','" + token + "','0');";
             return Json(address, JsonRequestBehavior.AllowGet);
        }

我假设问题是Angular认为Javascript为"unsafe"。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

为了在我的href中使用Javascript函数,我必须将以下内容添加到我的app.js文件中:

app.config([
    '$compileProvider',
    function ($compileProvider) {
        $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|chrome-extension|javascript):/);
    }
]);