从角度控制器调用mvc url.action

时间:2016-04-21 11:05:02

标签: angularjs asp.net-mvc

我有这个标签集:

    $scope.tabshomepage = [];
    $scope.tabshomepage.push(
    { title: 'Hjem', type: 'HOM', order: 1, active: true, disabled: false, color: 'black' },
    { title: 'Dirty', type: 'DIR', order: 2, active: false, disabled: false, color: 'purple' },
    { title: 'Dating', type: 'DAT', order: 3, active: false, disabled: false, color: 'green' },
    { title: 'Bloggers', type: 'SOC', order: 4, active: false, disabled: false, color: 'lblue' },
    { title: 'Konto', type: 'ACO', order: 5, active: false, disabled: false, color: 'black' },
    { title: 'Om os', type: 'ABU', order: 6, active: false, disabled: false, color: 'black' },
    { title: 'Kontakt og FAQ', type: 'COF', order: 7, active: false, disabled: false, color: 'black' }
);

它是在角度控制器中创建的:

$scope.homePageNavigate = function (type) {
    if(type == 'DIR'){
        //Perform a @Url.Action("Index", "Dirty", null)"
    }
    etc...
};

在选项卡上单击完成后,将执行homePageNavigate功能。

<script type="text/javascript">
    var map;
    var infowindow = new google.maps.InfoWindow({
        size: new google.maps.Size(150, 50)
    });
    var marker;

    function createMarker(latlng, html) {
        var contentString = html;
        var marker = new google.maps.Marker({
            position: latlng,
            map: map
        });

        google.maps.event.addListener(marker, 'click', function() {
            infowindow.setContent(contentString);
            infowindow.open(map, marker);
        });
    }

    function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
            center: {lat: 1.4683625, lng: 124.8308826},
            zoom: 15
        });
        var infoWindow = new google.maps.InfoWindow({map: map});

// Try HTML5 geolocation.
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (position) {
                var pos = {
                    lat: position.coords.latitude,
                    lng: position.coords.longitude
                };

                map.setCenter(pos);

                createMarker(map.getCenter(), "The marker is here");

            }, function () {
                handleLocationError(true, infoWindow, map.getCenter());
            });
        } else {
// Browser doesn't support Geolocation
            handleLocationError(false, infoWindow, map.getCenter());
        }
    }

    function handleLocationError(browserHasGeolocation, infoWindow, pos) {
        infoWindow.setPosition(pos);
        infoWindow.setContent(browserHasGeolocation ?
                'Error: The Geolocation service failed.' :
                'Error: Your browser doesn\'t support geolocation.');
    }
    google.maps.event.addDomListener(window, 'load', initMap);
</script>  

在那个功能区中我想调用一个mvc方法:@ Url.Action(&#34;索引&#34;,&#34; Dirty&#34;,null)&#34;,并返回一个视图(& #34;指数&#34)

解决此问题的最佳方法是什么? 任何解决方法?或者是一个简单的解决方案吗?

2 个答案:

答案 0 :(得分:4)

我之前通过在页面加载中通过剃刀执行一些配置来完成类似的操作:

<script>
    (function () {
        angular.module('App').value('paths', {
            home: '@Url.Action("Index", "Dirty", null)'
            // more paths here
        });
    })();
</script>

然后,您可以在角度应用中的任何位置注入并使用paths。 例如。在名为&#39; myCtrl`

的控制器内
angular.module('App').controller('myCtrl', ['paths', function(paths) {
    // you can use paths.home here
}]);

答案 1 :(得分:0)

使用AngularJS调用ASP.Net MVC Controller操作方法非常简单,如下所示

在Javascript中我写了

var app = angular.module(&#39; MyApp&#39;,[]);

angular.module(&#39; MyApp&#39;,[])

.controller(&#39; MyController&#39;,[&#39; $ scope&#39;,&#39; $ http&#39;,功能($ scope, $ http){

$ scope.search = function(){

$ HTTP({

方法:&#34; GET&#34;,

url:&#34; / home / GetData&#34;

})。then(function mySuccess(response){

$ scope.name = response.data;

},函数myError(response){

$ scope.name = response.statusText;

});

}

}]); 在HTML部分,我写了

<button ng-click="search()">Get Data</button>   

在Controller Action中,我写了返回Json(&#34;你抓住了AngularJS&#34;,JsonRequestBehavior.AllowGet);