javascript - angularjs变量赋值

查看:120
本文介绍了javascript - angularjs变量赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

 $scope.searchMsg={
     empName:$scope.search.name,
     empNum:$scope.search.num,
     startTime:$scope.search.startTimeValue,
     endTime:$scope.search.endTimeValue
         }

后边的$scope.search是一段封装的指令,大概类似这样

<lm-inputname></lm-inputname>
function lmInput_name() {
  return {
    restrict: 'E',
    scope: false,
    templateUrl: 'components/directiveTmp/lmInput_name.html',
    controller: function ($scope) {

    }
  }
}

下面是lmINput_name.html

<div ng-class="{true:'{{_init.nameCol}}',false:'col-sm-4'}[_init.nameCol!=undefined]">
  <div class="form-group">
    <label class="control-label">{{_init.name}}</label>
    <input  class="form-control" type="text" size="16" ng-model="search.name"
            placeholder="请输入{{_init.name}}">
  </div>
</div>

前面的$scope.searchMsg每次的属性可能不一样。
第一种:
想问的是有什么更好的方法给$scope.searchMsg赋值,看起来更优雅。
第二种:
或者可以提供一个方法在<lm-inputname set-model="来代替search.name"></lm-inputname>上再添加一个set-model的属性指令,可以每次动态定义不同的ng-model来代替search.name。
能做到第二种更好,初学者,望指教。

解决方案

<lm-inputname search-model="search.name"></lm-inputname>
function lmInput_name() {
  return {
    restrict: 'E',
    scope: {
        searchModel:'='  // =双向绑定,@绑定字符串,&绑定表达式
    },
    templateUrl: 'components/directiveTmp/lmInput_name.html',
    controller: function ($scope) {

    }
  }
}

这篇关于javascript - angularjs变量赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆