在angular js中使用ng点击的嵌套控制器

Nested Controller with ng-click in angular js

本文关键字:嵌套 控制器 ng angular js      更新时间:2023-09-26

我有一个Item控制器,用户控制器可以应用到它。所以我创建了apply控制器来实现这一点,但问题是我有Item视图页面,我必须从那里将Item id传递给apply控制器的应用方法。

我的页面

<div class="container_12" ng-controller="ItemCtrl" ng-init="profile()">
    <div class="gallery" >
        <ul class="unstyled">
            <h1>{{item.name}}</h1>
            <br>
                <ul>
                    <li>About: {{item.description}}</li>
                </ul>
            </ul>
            <br>
                <div  ng-controller="ApplyCtrl">
                    <button ng-click="apply(item._id)">Show Interest</button>
                </div>
            </div>
        </div>

所以我想要嵌套的ng-controller,因为我希望传递项id,但为此我的控制台显示错误。

Error: [ng:areq] http://errors.angularjs.org/1.4.8/ng/areq?p0=ApplyCtrl&p1=not%20a%20function%2C%20got%20undefined
    at Error (native)
    at http://localhost:8080/vendor/angular/angular.min.js:6:416
    at qb (http://localhost:8080/vendor/angular/angular.min.js:22:131)
    at Qa (http://localhost:8080/vendor/angular/angular.min.js:22:218)
    at http://localhost:8080/vendor/angular/angular.min.js:80:210
    at w (http://localhost:8080/vendor/angular/angular.min.js:60:177)
    at D (http://localhost:8080/vendor/angular/angular.min.js:61:30)
    at g (http://localhost:8080/vendor/angular/angular.min.js:55:105)
    at g (http://localhost:8080/vendor/angular/angular.min.js:55:122)
    at D (http://localhost:8080/vendor/angular/angular.min.js:62:134)(anonymous function) @ angular.js:12520(anonymous function) @ angular.js:9292r.$apply @ angular.js:16157(anonymous function) @ angular.js:1679e @ angular.js:4523c @ angular.js:1677yc @ angular.js:1697Zd @ angular.js:1591(anonymous function) @ angular.js:29013c @ jquery.js:3p.fireWith @ jquery.js:3x.extend.ready @ jquery.js:3q @ jquery.js:3 

我有什么办法做到这一点吗?

短期-根据控制台错误,您的AngularJS似乎甚至无法识别ApplyCtrl-我会确认正在加载实例化ApplyCtrr的javascript文件。

长期-你基本上想要实现的是一个嵌套视图-我强烈建议你看看UI路由器,它可以准确地处理你想要实现的内容。