Programing

컨트롤러 함수 호출이 작동하지 않는 각도 ng-click

lottogame 2020. 10. 22. 07:39
반응형

컨트롤러 함수 호출이 작동하지 않는 각도 ng-click


다른 컨트롤러에 변수를 전달해야합니다. ListCtrl과 관련된 다음 코드가 있습니다.

<a href="#items" data-router="article" ng-click="changeListName('metro')">

링크는 다른 컨트롤러 인 ItemCtrl으로 이동합니다.

ItemCtrl에 변수를 전달하고 싶습니다. SharedProperties라는 서비스를 사용하려고 생각했습니다.

service('sharedProperties', function () {
    var list_name = '';

    return {
        getListName: function() {
            return list_name;
        },
        setListName: function(name) {
            list_name = name;
        }
    };
});

링크를 클릭하면 각도 클릭 이벤트를 호출하여 다음 기능을 트리거합니다.

$scope.changeListName = function(name) {
    sharedProperties.setListName(name);
};

그러나 ng-click이 내 공유 자산의 가치를 변경하지 않는 것 같습니다.

최신 정보

ng-click에 의해 트리거되는 함수 내부에 경고를 넣으면 경고가 트리거됩니다.

그러나 다음과 같이 내 함수를 작성할 때 :

$scope.changeListName = function(name) {
    sharedProperties.setListName(name);
};

작동하지 않습니다 ... 'sharedProperties.setListName (name);'처럼 보입니다. 실행되지 않습니다 ...

더미 이름 (예 : metro)을 사용하여 함수 외부에두면 작동합니다 ..

업데이트 3

나는 여러 가지를 시도했고 문제 가이 기능에 있다고 확신합니다.

$scope.changeListName = function(list_name) {
    sharedProperties.setListName(list_name);
};

왜 이런 일이 일어나는지 아십니까?


ngClick 과 함께 ngHref 지시문을 사용해야 할 것입니다 .

 <a ng-href='#here' ng-click='go()' >click me</a>

예 : http://plnkr.co/edit/FSH0tP0YBFeGwjIhKBSx?p=preview

<body ng-controller="MainCtrl">
    <p>Hello {{name}}!</p>
    {{msg}}
    <a ng-href='#here' ng-click='go()' >click me</a>
    <div style='height:1000px'>

      <a id='here'></a>

    </div>
     <h1>here</h1>
  </body>

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

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';

  $scope.go = function() {

    $scope.msg = 'clicked';
  }
});

이것이 사용중인 라이브러리에서 작동하는지 모르겠지만 적어도 ngClick 기능을 연결하고 사용할 수 있습니다.

** 업데이트 **

다음은 세트의 데모이며 서비스로 잘 작동합니다.

http://plnkr.co/edit/FSH0tP0YBFeGwjIhKBSx?p=preview

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

app.controller('MainCtrl', function($scope, sharedProperties) {
  $scope.name = 'World';

  $scope.go = function(item) {
    sharedProperties.setListName(item);


  }

  $scope.getItem = function() {

    $scope.msg = sharedProperties.getListName();
  }
});

app.service('sharedProperties', function () {
    var list_name = '';

    return {

        getListName: function() {
            return list_name;
        },
        setListName: function(name) {
            list_name = name;
        }
    };


});

* 편집하다 *

Please review https://github.com/centralway/lungo-angular-bridge which talks about how to use lungo and angular. Also note that if your page is completely reloading when browsing to another link, you will need to persist your shared properties into localstorage and/or a cookie.


Use alias when defining Controller in your angular configuration. For example: NOTE: I'm using TypeScript here

Just take note of the Controller, it has an alias of homeCtrl.

module MongoAngular {
    var app = angular.module('mongoAngular', ['ngResource', 'ngRoute','restangular']);

    app.config([
        '$routeProvider', ($routeProvider: ng.route.IRouteProvider) => {
            $routeProvider
                .when('/Home', {
                    templateUrl: '/PartialViews/Home/home.html',
                    controller: 'HomeController as homeCtrl'
                })
                .otherwise({ redirectTo: '/Home' });
        }])
        .config(['RestangularProvider', (restangularProvider: restangular.IProvider) => {
        restangularProvider.setBaseUrl('/api');
    }]);
} 

And here's the way to use it...

ng-click="homeCtrl.addCustomer(customer)"

Try it.. It might work for you as it worked for me... ;)


I'm going to guess you aren't getting errors or you would've mentioned them. If that's the case, try removing the href attribute value so the page doesn't navigate away before your code is executed. In Angular it's perfectly acceptable to leave href attributes blank.

<a href="" data-router="article" ng-click="changeListName('metro')">

Also I don't know what data-router is doing but if you still aren't getting the proper result, that could be why.

참고URL : https://stackoverflow.com/questions/17457024/angular-ng-click-with-call-to-a-controller-function-not-working

반응형