触发角度功能

时间:2015-11-20 10:19:18

标签: javascript jquery angularjs

我有跟随控制器的功能,点击链接后应触发该功能:

<a id="1" href="" name="xxx" ng-click="searchall(id)">sample link</a>

ng.controller('SearchResultController', ['$scope', '$stateParams', 'Service', function ($scope, $stateParams, Service) {
    $scope.searchall = function () {
        $scope.SearchResult = Service.result;

        Service.searchall({ id: $stateParams.id }, function (data) {
            alert('SearchResultController');
            $scope.SearchResult = data;
        });        
    };
}]);

当我发表评论时:

Service.searchall({ id: $stateParams.id }, function (data) {             
    $scope.SearchResult = data;
});

警报显示在浏览器中,因此我的控制器与视图链接但我仍然需要在服务上调用该功能。我做错了什么?

2 个答案:

答案 0 :(得分:0)

您应该检查值$stateParams.id。如果这个不正确并且您在控制台404中看到错误,那么您的成功处理程序不会引发。如果添加错误处理程序,则可以看到管道工作正常

Service.searchall({ id: $stateParams.id }, function (data) {
    alert('SearchResultController');
    $scope.SearchResult = data;
},function(){ //error handler
    alert('Something wrong');
});  

答案 1 :(得分:0)

如果没有更多细节,很难提供确凿的答案,所以这里是一些传递给Angular函数的变量的示例:https://jsfiddle.net/jk0peh4h/1/

<div ng-app="myApp" ng-controller="myCtrl">First Name:
    <input type="text" ng-model="firstName">
    <br>Last Name:
    <input type="text" ng-model="lastName">
    <button ng-click="testFunction(firstName, lastName)">Test</button>
</div>
    <script>
        var app = angular.module('myApp', []);
        app.controller('myCtrl', function($scope) {
            $scope.testFunction = function(firstName, lastName) {
                alert(firstName + " " + lastName);
            };
        });
    </script>

很难知道这是否有帮助,但您可能需要查阅文档:https://docs.angularjs.org/api/ng/directive/ngClick