【问题标题】:parent directive scope → child directive scope父指令范围 → 子指令范围
【发布时间】:2014-08-25 01:28:09
【问题描述】:

我有 angularjs 问题。

我想在 $index 上像 ng-repeat 一样。

看看函数'updateScope'

var updateScope = function(scope, index, valueIdentifier, value, keyIdentifier, key, arrayLength){
    // TODO(perf): generate setters to shave off ~40ms or 1-1.5%
    scope[valueIdentifier] = value;
    if (keyIdentifier) scope[keyIdentifier] = key;
    scope.$index = index;
    scope.$first = (index === 0);
    scope.$last = (index === (arrayLength - 1));
    scope.$middle = !(scope.$first || scope.$last);
    // jshint bitwise: false
    scope.$odd = !(scope.$even = (index&1) === 0);
    // jshint bitwise: true
};

此代码操作 '父指令作用域变量 -> 子指令作用域变量'

你可以这样做

ng-repeat(...)
    {{$index}}

我要这个动作!!

我尝试,但不采取行动

这是我的代码

-玉

m-progressbar(min="0", max="300", ng-model="value")
    label {{$ratio}}

-js

$scope.value=72;

-指令

define([
    'jquery', 'underscore', 'angular', 'jquery-ui'],
function ($, _, angular) {
    var progressbarApp = angular.module('directives.progress_bar', []);
    progressbarApp
    .directive('mProgressbar', [function mProgressbar () {
     return {
            templateUrl: '/directives/progress-bar.jade',
            restrict: 'EA',
            scope: {
                min: '=',
                max: '=',
                model: "=ngModel"
            },
            link: function (scope, iElement, iAttrs) {
                var progressbar = $( "#progressbar" ); 
                var value = scope.model;
                scope.$watch('model',function (newValue, oldValue, scope){
                    var ratio = ((newValue - scope.min) / (scope.max - scope.min)) * 100;
                    progressbar.progressbar({value:ratio});
                    scope.$ratio = ratio;
                });
            }
        };
    }]) 
}); 

我的代码点是

  • scope.$ratio = ratio;

请告诉我一个答案 ㅠoㅠ

【问题讨论】:

    标签: angularjs angularjs-directive


    【解决方案1】:

    如果您想在模板中使用 {{$ratio}},我认为您只是忘记在 ratio 之前添加 $。
    我的意思是你应该像下面这样写。

    scope.$ratio = ratio;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-12
      • 1970-01-01
      相关资源
      最近更新 更多