角中继器和首次尝试使用角

时间:2018-08-31 16:26:21

标签: asp.net angularjs model-view-controller dotnetnuke repeater

我试图在ASP.Net/DotNetNuke MVC项目中第一次使用angular。

查看:

@inherits DotNetNuke.Web.Mvc.Framework.DnnWebViewPage<IEnumerable<IMT.AdvancedProductSearchMVC.Models.SearchTerm>>
@using DotNetNuke.Web.Mvc.Helpers

<script src="/DesktopModules/RazorCart/Core/Content/Scripts/angular.min.js"></script>

<div ng-app="APSApp" class="container">
    <br />
    <br />
    <input type="text" placeholder="Search Terms" ng-model="searchTerms" />
    <br />
    <div ng-controller="apsCtl">
        <table class="table">
            <tr ng-repeat="r in searchTerms">
                <td>{{r.DisplayText}}</td>
                <td><input type="text" ng-model="r.SearchInput"></td>
            </tr>
        </table>
    </div>
</div>

<script src="DesktopModules/MVC/AdvancedProductSearchMVC/Scripts/AdvancedProductSearch.js"></script>

AdvancedProductSearch.js

 aps = angular.module('APSApp', []);
aps.controller('apsCtl',
    function(APSService, Features, $scope, $http) {
        alert("calling factory");
        $scope.searchTerms = [];
        APSService.getAPS(successFunction, failureFunction);

        successFunction = function(data) {
            alert("success");
            $scope.searchTerms = data;
        };

        failureFunction = function(data) {
            alert("failure");
        };
    });

aps.factory('APSService',
    function($http) {
        alert("getting factory");
        return {
            getAPS: function(onSuccess, onFailure) {
                //var APSService = {};
                $http.get('/Desktopmodules/MVC/AdvancedProductSearchMVC/AdvancedProductSearch/GetAPS').success(onSuccess).error(onFailure);
            }
        };
    });

当然,它不起作用。例如,我的控制器从未被调用过,我也不知道自己在做什么。 我如何更正我的代码才能完成这项工作?

2 个答案:

答案 0 :(得分:0)

您的AdancedProductSearch.js具有不同的模块名称,并且您在HTML中使用了不同的模块名称。

在脚本中重命名模块名称,并添加一个空的依赖项,如下所示

  angular
        .module('APSApp',[])
        .controller('apsCtl', apsCtl)
    ;

答案 1 :(得分:0)

经过数小时的阅读角度文档和示例以及大量测试后,它现在可以正常工作:

我的AdvancedProductSearch.js

var aps = angular.module("APSApp", []);
aps.config(function($httpProvider) {
    $httpProvider.defaults.transformRequest = function(data) {
        return data !== undefined ? $.param(data) : null;
    };
});

aps.factory('SearchTerms', 
    function($http) {
        return {
            getSearchTerms: function(onSuccess, onFailure) {
                const rvtoken = $("input[name='__RequestVerificationToken']").val();
                $http({
                    method: "post",
                    url: "/DesktopModules/MVC/AdvancedProductSearchMVC/AdvancedProductSearch/GetAPS",
                    headers: {
                        "ModuleId": moduleId,
                        "TabId": tabId,
                        "RequestVerificationToken": rvtoken
                    }
                }).success(onSuccess).error(onFailure);
            }
        };
    });

aps.controller('APSCtl',
    function(SearchTerms, $scope) {
            $scope.searchTerms = [];
            successFunction = function(data) {
                $scope.searchTerms = data;
                console.log($scope.searchTerms);
            };

            failureFunction = function(data) {
                console.log('Error' + data);
            };
            SearchTerms.getSearchTerms(successFunction, failureFunction);
    });