简体   繁体   English

Angular $ scope函数在ng-repeat之外不起作用

[英]Angular $scope function not working outside of ng-repeat

I have a <table> element, at which I declared a controller (only one in the app at the moment). 我有一个<table>元素,我在其中声明了一个控制器(目前在应用程序中只有一个)。 I also have a ng-repeat on <tr> in the <tbody> element, which is working just fine, creating multiple table rows as intended. 我在<tbody>元素中的<tr>上也有一个ng-repeat ,它工作得很好,按照预期创建多个表行。 In the controller i have some api calls which are called for single table rows and which work just fine, and a function called from one of the <th> 's in <thead> , which I can't get to work. 在控制器中,我有一些api调用,它们被调用为单个表行,并且工作得很好,以及从<thead>中的<th>之一调用的函数,我无法工作。 I know it's a scope thing, but I just can't grasp what I'm doing wrong. 我知道这是范围的事情,但我无法理解我做错了什么。

To sum it up: 把它们加起来:

simplified html fragment: 简化的html片段:

<table ng-controller='myController'>
    <thead>
         <tr>
             <th>Username</th>
             <th><button ng-click='doStuff()'>Do stuff</button></th>
         </tr>
    </thead>
    <tbody>
         <tr ng-repeat='user in users'>
             <td>{{user.name}}</td>
             <td><button ng-click='delete(user)'>Delete</button>
         </tr>
    </tbody>
</table>

simplified js fragment: 简化的js片段:

app.controller('myController', ['$scope', '$http', users, function($scope, $http, users) {
    //api call to get users, working fine
    users.getAll().success(function(data) {
        $scope.users = data;
    });

    //api call to delete users, also working fine
    $scope.delete = function(user) {
        users.delete(user).success(function() {});
    };

    //can't get this to fire
    $scope.doStuff = function() {
        alert('I do stuff');
    };
}]);

Any insight would be helpful, thanks in advance! 任何见解都会有所帮助,在此先感谢!

EDIT 编辑

I assume the issue is comming from one of the modules, so I copied the whole thing into a plnkr . 我假设问题来自其中一个模块,所以我把整个事情复制到一个plnkr中 Sorry for the styling, removed it for more code simplicity. 对于样式感到抱歉,删除它以获得更简单的代码。

EDIT 2 编辑2

After studying the plnkr I came to see that it was an unclosed <div> element in the <thead> . 在研究了plnkr后,我发现它是<thead>未闭合的<div>元素。 Ouch. 哎哟。 Thanks for the replies, and please excuse my carelessness. 感谢您的回复,请原谅我的粗心大意。

EDIT 3 编辑3

An even more carefull study revealed, that the problem has in fact been also lying in poorly designed jasmine unit tests. 一项更为细致的研究表明,问题实际上也存在于设计不良的茉莉花单元测试中。

It appears to be firing. 它似乎在解雇。 I've had to simulate the asynchronous loading of users , but that shouldn't make a difference. 我必须模拟users的异步加载,但这不应该有所作为。 Can you add a snippet to demonstrate the issue? 你能添加一个片段来证明这个问题吗?

  angular.module('app', []).controller('myController', ['$scope', '$http', '$timeout', function($scope, $http, $timeout) { //api call to get users, working fine $timeout(function() { $scope.users = [{ name: 'A' }, { name: 'B' }]; }, 1000); //api call to delete users, also working fine $scope.delete = function(user) { console.log('delete', user); }; $scope.doStuff = function() { console.log('I do stuff'); }; } ]); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.js"></script> <div ng-app="app"> <table ng-controller='myController'> <thead> <tr> <th>Username</th> <th> <button ng-click='doStuff()'>Do stuff</button> </th> </tr> </thead> <tbody> <tr ng-repeat='user in users'> <td>{{user.name}}</td> <td> <button ng-click='delete(user)'>Delete</button> </tr> </tbody> </table> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM