angularjs----mark

watch parse 手动解析

http://jsbin.com/rasafitame/edit?html,js,output


<html ng-app='myApp'>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.js">script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bintitle>
head>
<body>
  {{1}}
  <div ng-controller='myController'>
    <input type="text" ng-model='expr' placeholder="请输入值">
    <h2>解析值{{parsedValue}}h2>
  div>
body>
html>
var app = angular.module('myApp',[]);
app.controller('myController',['$scope','$parse',function($scope,$parse){
  $scope.$watch('expr',function(oldVal,newVal,scope){
    if(oldVal !== newVal){
      var parseFun = $parse('expr');
      $scope.parsedValue = parseFun(scope);
    }
  });
}]);

你可能感兴趣的:(angualr,javascript)