简体   繁体   English

嵌套模板上的Angular js范围

[英]Angular js scope on nested template

I have the following directive where I have been using as 我一直在使用以下指令

<ui-submit-button btn-state='btnSendProcessing' />

The btnSendProcessing is a value in the controller's scope it gets turned on and off while the http request is being fired. btnSendProcessing是控制器范围内的一个值,在触发http请求时会被打开和关闭。

The ng-disabled works but it is not showing the processing icon. ng-disabled作品,但未显示处理图标。

.directive('uiSubmitButton', function() {

    return {
      restrict: 'E',
      template : function() {
        return ' \
          <button class="btn btn-success" type="submit" ng-disabled="btnState"> \
            <input type="checkbox" ng-model="btnState" /> {{btnState}} \
            <i class="fa fa-cog" ng-show="btnState" /> \
            <span ng-hide="btnState">{{btnLabel || "Submit"}}</span> \
          </button>';
      },
      replace : true,
      scope : {
        btnState : '=',
        btnLabel : '@'
      }
    };

  })

What could be the issue here? 这里可能是什么问题?

Fiddle is working as expected http://jsfiddle.net/to5y9kud/1/ 小提琴正在按预期工作http://jsfiddle.net/to5y9kud/1/

in scope inheritance, and basically in any js prototype inheritance, if you use a primitive, it copies the primitive value to the inherited object. 在作用域继承中,基本上在任何js原型继承中,如果您使用基本体,它将基本体值复制到继承的对象。

if you want changes to effect both the parent and the inherited scope, you should use an Array or an Object, this is an example which explains what is a normal behavior of an inherited scope: 如果要使更改同时影响父范围和继承的范围,则应使用Array或Object,这是一个示例,解释了继承范围的正常行为:

$parentScope.a = 1;
$parentScope.b = {a:1};
$childScope = $parentScope.$new() //inherit from $parentScope

console.log($childScope.a === 1, $childScope.b.a === 1) //true, true
$childScope.a = 2;
$childScope.b.a = 2;
console.log($parentScope.a === 2, $parentScope.b.a === 2) //false, true

so in your case btnSendProcessing should be an object and not a primitive. 因此,在您的情况下, btnSendProcessing应该是一个对象,而不是基元。 here is a working fiddle: http://jsfiddle.net/avp9ye1g/1/ 这是一个工作的小提琴: http : //jsfiddle.net/avp9ye1g/1/

read this great answer for better understanding: Understanding Javascript immutable variable ; 阅读这个很棒的答案可以更好地理解: 了解Javascript不可变变量

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

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