简体   繁体   English

ion-content和ion-footer具有不同的$ scope

[英]ion-content and ion-footer have different $scope

I have two input fields inside my ion-content and they both have an ng-model attached to them. 我的离子含量内部有两个输入字段,并且它们都附加有ng模型。 Then inside my ion-footer I have an ng-click where I call a function and pass in the two ng-models. 然后在我的ion-footer中,我有一个ng-click,在其中调用函数并传入两个ng-model。

This all worked fine when I had the ng-click inside the ion-content, but when I move it to the footer I get undefined for the two parameters I pass to the function. 当我在离子含量内部进行ng-click时,这一切都很好,但是当我将其移至页脚时,对于传递给函数的两个参数,我不确定。

So does this mean that ion-content and ion-footer have different $scope's? 那么这是否意味着离子含量和离子页脚具有不同的$ scope? Even though they're in the same file and have the same controller?? 即使它们位于同一文件中并且具有相同的控制器?

I believe ion-footer & ion-content creates new child scope which is Prototypically inerherit from current scope. 我相信ion-footerion-content会创建新的子范围,这在原型上是继承于当前范围的。 Below ionic code will give you better illustration that how it works internally, the scope: true, is responsible for creating a new child scope. 下面的离子代码将为您更好地说明它如何在内部工作,即scope: true,负责创建新的子作用域。

Code

.directive('ionContent', [
  '$parse',
  '$timeout',
  '$ionicScrollDelegate',
  '$controller',
  '$ionicBind',
function($parse, $timeout, $ionicScrollDelegate, $controller, $ionicBind) {
  return {
    restrict: 'E',
    replace: true,
    transclude: true,
    require: '^?ionNavView',
    scope: true, //<-- this creates a prototypically inerherited scope
    template:
    '<div class="scroll-content">' +
      '<div class="scroll"></div>' +
    '</div>',

You need to use . 您需要使用. annotation will fix your problem 注解将解决您的问题

Eg. 例如。

If you are using variable as primitive like 如果您将变量用作原始变量,例如

$scope.volume = 5

Then you need to use: 然后,您需要使用:

$scope.data = { 'volume' : 5}

Angular Prototypal Scope Inheritance 角度原型作用域继承

Explanation of the answer in the comments by pankajparkar: pankajparkar在评论中回答的解释:

the ion-content directive has its new scope. 离子含量指令具有新的范围。 It works using the dot notation (important when dealing with scope inheritance) 它使用点表示法(在处理范围继承时很重要)

That is why it works with ng-model="data.model1 这就是为什么它可以与ng-model =“ data.model1

Please refer to: 请参阅:

AngularJS documentation on scopes 范围内的AngularJS文档

Egghead video 蛋头视频

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

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