Ng-click does not start from a template

In AngularJS, methods can be called from within templates without a directive or routeProvider to bind a region?

My particular problem is that my main controller (AppCtrl) is located on the body of my index page. In the index, I use ngView to pull patterns according to routing. Each routeProvider has its own controller, with the exception of the default template. I suggested that due to region inheritance, I could use the methods defined using AppCtrl from the template, but I'm trying to just make a simple alert or console.log is not registering anything. I know that the $ region fits the pattern because my data is displayed correctly.

This seems to be a problem with $ scope, but I'm not sure how to solve it. Do I need to create a directive to explicitly bind them?

index.html

<body ng-controller="AppCtrl"> <div ng-view></div> </body> 

app.js

 var myApp = angular.module('myApp', []) .config(function($routeProvider) { .when('/', { templateUrl: 'partials/list.html' }), .when('/info/:id, { templateUrl: 'partials/info.html' controller: 'InfoCtrl' }); }); 

controllers.js

 var controllers = {}; controllers.AppCtrl = function($scope, $location, Factory) { ... $scope.doSomething = function() { alert('hello'); }; }; myApp.controller(controllers); 

list.html

 <a href="#" ng-click="doSomething()">Do Something</a> // no response 
+4
source share
1 answer

I seem to have found the root of my problem. As described, using the function really worked. When I returned to solving my specific problem, the function again refused to work. It turns out that this is not a scale problem, but a binding problem. The example I gave was inaccurate, but helped me isolate the problem. I will demonstrate:

 <!-- alert fires, tries to find '#' in location, binded values do not update --> <a href="#" ng-click="addOne()">Add 1</a> <!-- key binding does not update --> <a href="#" ng-click="key = key+1" ng-init="key=0">Add 1</a> <!-- key updates --> <a ng-click="key = key+1" ng-init="key=0">Add 1</a> 

For some reason, including href, the bound value was prevented from being updated. Does anyone know if there is a "preventDefault" that can be used as a workaround so that hrefs can be turned on for verification? This seems to be either a bug or ng-click is for button use only.

+3
source

Source: https://habr.com/ru/post/1483445/


All Articles