简体   繁体   English

将角度指令与控制器链接

[英]Linking angular directive with controller

Been following through some Angular JS tutorials and I'm trying to translate them into the Ionic framework but running into some problems. 遵循了一些Angular JS教程,我试图将它们转换为Ionic框架,但遇到了一些问题。 I'm trying to write a reusable HTML control but the model is not being bound to the view. 我正在尝试编写可重用的HTML控件,但该模型未绑定到视图。 Here is my code: 这是我的代码:

//App.js


   angular.module('starter', ['ionic', 'starter.controllers', 'starter.directives'])
.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider

      .state('app', {
      url: '/app',
      abstract: true,
      templateUrl: 'templates/menu.html'
     })

    .state('app.playlists', {
      url: '/playlists',
      views: {
        'menuContent': {
          templateUrl: 'templates/playlists.html',
          controller: 'PlaylistsCtrl'
        }
      }
    })


  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/app/playlists');
});

//Controller.js
angular.module('starter.controllers', [])


.controller('PlaylistsCtrl', function($scope) {

  $scope.playlists = [
    { title: 'Reggae', id: 1 },
    { title: 'Chill', id: 2 },
    { title: 'Dubstep', id: 3 },
    { title: 'Indie', id: 4 },
    { title: 'Rap', id: 5 },
    { title: 'Cowbell', id: 6 }
  ];
})

//Directives.js
angular.module('starter.directives', [])

.directive('testInfo', function(){
    return {
        restrict: 'E',
        scope: {
           info: '='
        },
        templateUrl: 'templates/test_view.html'
    };
});

//Test View

<button class="item ion-item" >
    The playlist title is + {{playlist.title}}
</button>

//App View 

<ion-content>
    <ion-list>
      <ion-item ng-repeat="playlist in playlists" >
        <div ng-click="playListSelected($index)">
             <test-info info="playlist"></test-info>
        </div>
      </ion-item>
    </ion-list>
  </ion-content>
</ion-view>

//Index.html //Index.html

I know I'm linking my js files correctly, however in the custom view the playlist.title never has a value. 我知道我正确地链接了我的js文件,但是在自定义视图中,playlist.title从来没有值。 The controller never seems to bind to the html element. 控制器似乎从未绑定到html元素。 Double checking some angular tutorials I was going through, I'm following a similar approach and can't seem to figure out what the problem is. 仔细检查我正在经历的一些角度教程,我遵循的是类似的方法,似乎无法弄清楚问题出在哪里。

In your directive you are defining a value on the directive's scope named info . 在指令中,您正在指令的范围内定义一个名为info So inside the template for the directive, you need to reference that with the name info , not playlist . 因此,在指令的模板内部,您需要使用名称info而不是playlist进行引用。

<button class="item ion-item" >
    The playlist title is + {{info.title}}
</button>

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

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