1

This is the HTML

<div class="container">          
      <div class="row">
          <div class="col-md-12">

              <div ng-controller="urlController">

                <input type="text" ng-model="imgUrl" />         

                <button ng-click="viewPath()">View</button>
                <button ng-click="setPath()">Save</button>

                <img ng-src="{{ newUrl }}" />  
              </div>

          </div>
      </div>

This is my angular code:

var mySettings = angular.module('mySettings',[]);

mySettings.controller('urlController', ['$scope', '$location', '$log', function($scope, $location, $log){

    $scope.imgUrl = '';

    $scope.viewPath = function (){
        $scope.newUrl = $scope.imgUrl;
    }

    $scope.setPath = function () {
        var myPath = $location.path($scope.newUrl);

        $log.info(myPath);
    };

Explanation:

Here I have a input tag to put the absolute link of image and image tag which will show the image when i press view button.

Now there is another button that is save which i want to perform a simple function that is to save/update the link pasted in input box into the image source. So the next time if I press refresh, it should show me the linked image as its source.

Problem: If I paste a local link eg. d:\bla.png img src tag says unsafe. If I paste a http link it show the image in image tag but does not save on pressing the save button to $scope.newUrl object. So if I press refresh the image is gone.

Need solution and if there is a post which can explain the procedure step by step it will be great.

Sam
  • 481
  • 3
  • 9
  • 22
  • 3
    use ng-src instead of src – sdfacre Jan 14 '16 at 05:38
  • i tried that also...but my question is not about ng-src – Sam Jan 14 '16 at 05:41
  • what do you mean by refresh? which refresh button did you click? the browser one? – sdfacre Jan 14 '16 at 05:44
  • yes the browser refresh or F5 – Sam Jan 14 '16 at 06:09
  • and your question is how you can store the newUrl so it retains after you refresh the page? – sdfacre Jan 14 '16 at 06:19
  • yes i want to store the value so it retains after you refresh the page – Sam Jan 14 '16 at 06:29
  • If that's the case, you need to look at a data storage option, i.e. server backend, database, cookie, session storage or local storage. this example http://stackoverflow.com/questions/12940974/maintain-model-of-scope-when-changing-between-views-in-angularjs/16559855#16559855 shows how to use session storage. – sdfacre Jan 14 '16 at 11:53
  • I also created this https://plnkr.co/edit/1wFzQeqeZcDaY8SDRiVo?p=preview for you that uses local storage. – sdfacre Jan 14 '16 at 12:03

0 Answers0